Skip to content

Instantly share code, notes, and snippets.

@dkapitan
Created January 16, 2018 20:59
Show Gist options
  • Save dkapitan/3a2018577c82516b1d15e307cdff60ff to your computer and use it in GitHub Desktop.
Save dkapitan/3a2018577c82516b1d15e307cdff60ff to your computer and use it in GitHub Desktop.
Load CBS data into PostgreSQL database
# download file from https://www.cbs.nl/nl-nl/dossier/nederland-regionaal/geografische%20data/wijk-en-buurtkaart-2017
import shapefile
import pandas as pd
from sqlalchemy import create_engine
db = create_engine('postgresql://localhost:5432/opendata')
sf = shapefile.Reader("/Users/dkapitan/opendata/CBS_PC4_2017/CBS_PC4_2017_v1.shp")
shapes = sf.shapes()
records = sf.records()
cols = [field[0] for field in sf.fields][1:]
df = pd.DataFrame.from_records(records, columns=cols)
df.to_sql('pc4', db, schema='cbs', if_exists='replace', index=False, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment