Skip to content

Instantly share code, notes, and snippets.

@tomplex
Created April 26, 2022 13:35
Show Gist options
  • Save tomplex/860380e4ef885cff5579cf6c16634bdc to your computer and use it in GitHub Desktop.
Save tomplex/860380e4ef885cff5579cf6c16634bdc to your computer and use it in GitHub Desktop.
proposed workflow for loading surface geometry per https://github.com/shapely/shapely/discussions/1369
from geomet import wkt
from io import StringIO
import psycopg2
def main():
# we'll use this to copy data into postgres
fileobj = StringIO()
# build WKT geometries
for idx, surface in enumerate(my_surfaces, start=1):
geojson = {
'type': 'polyhedralsurface',
'coordinates': [[
[triangle] for triangle in surface
]]
}
# this will not work yet - would require work from geomet library
ewkt = f"SRID=4326;{wkt.dumps(geojson)}"
fileobj.write(f"{idx},{ewkt}\n")
# seek back to the beginning of the file object
fileobj.seek(0)
sql = """
create table surfaces_tmp(
id int,
surface geometry(PolyhedralSurface, 4326)
);
"""
with psycopg2.connect(**params) as conn:
cursor = conn.cursor()
cursor.execute(sql)
# copy will load lots of data into postgres very quickly.
cursor.copy_expert('copy surfaces_tmp from stdin with csv', fileobj)
# you can also perform any other required post-processing here with postgis functions.
cursor.execute('insert into <my table>(surface_geometry_column) select surface from surfaces_tmp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment