Browse Source

Insert into PostGIS

master
popovspiridon99 2 months ago
parent
commit
6afe9ceb05
  1. 26
      init.py

26
init.py

@ -8,7 +8,7 @@ DB_CONFIG = {
"dbname": "ems", "dbname": "ems",
"user": "ems", "user": "ems",
"password": "qFadbNBSaEoc", "password": "qFadbNBSaEoc",
"host": "172.16.1.210",
"host": "localhost",
"port": 5432, "port": 5432,
} }
@ -17,15 +17,31 @@ ROOT_DIR = "./"
def insert_bounds(entity_id, entity_type, geometry, conn): def insert_bounds(entity_id, entity_type, geometry, conn):
"""Insert data into the bounds table.""" """Insert data into the bounds table."""
published_at = datetime.now()
try: try:
with conn.cursor() as cur: with conn.cursor() as cur:
# Check if the GeoJSON is a GeometryCollection
if geometry.get('type') == 'GeometryCollection':
# Extract polygons from the GeometryCollection and convert them to MultiPolygon
geometry = {
"type": "MultiPolygon",
"coordinates": [
geom["coordinates"] for geom in geometry["geometries"] if geom["type"] == "Polygon"
]
}
# Insert into the bounds table
cur.execute( cur.execute(
""" """
INSERT INTO bounds (entity_id, entity_type, geometry, published_at)
VALUES (%s, %s, %s, %s);
INSERT INTO bounds (entity_id, entity_type, geometry)
VALUES (%s, %s,
ST_Transform(
ST_Multi(
ST_GeomFromGeoJSON(%s::JSON)
),
3857)
);
""", """,
(entity_id, entity_type, json.dumps(geometry), published_at)
(entity_id, entity_type, json.dumps(geometry))
) )
conn.commit() conn.commit()
except Exception as e: except Exception as e:

Loading…
Cancel
Save