Insert into PostGIS
This commit is contained in:
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)
|
INSERT INTO bounds (entity_id, entity_type, geometry)
|
||||||
VALUES (%s, %s, %s, %s);
|
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:
|
||||||
|
Reference in New Issue
Block a user