Skip to content

Instantly share code, notes, and snippets.

@tastatham
Last active February 24, 2020 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tastatham/d436aa99b8d98263a3856fca75c69bfd to your computer and use it in GitHub Desktop.
Save tastatham/d436aa99b8d98263a3856fca75c69bfd to your computer and use it in GitHub Desktop.
Geopandas to postgis example
# Load pkgs and data
import geopandas as gpd
data = gpd.read_file("https://gist.githubusercontent.com/HTenkanen/456ec4611a943955823a65729c9cf2aa/raw/be56f5e1e5c06c33cd51e89f823a7d770d8769b5/ykr_basegrid.geojson")
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Subset argentina as single ply
argentina = world[world['name'] == 'Argentina' ]
# Define engine
engine = create_engine("postgresql+psycopg2://[user]:[password]@localhost:5432/[my_db]")
# Copy to postgis
copy_to_postgis(data, engine, table='my_table1', schema='my_schema', if_exists='replace')
# Copy to postgis
copy_to_postgis(world, engine, table='my_table2', schema='my_schema', if_exists='replace')
# Copy to postgis
copy_to_postgis(argentina, engine, table='my_table3', schema='my_schema', if_exists='replace')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment