[GIS] How to save a newly created shapefile in PostGIS

postgispythonshapefile

I am creating shapefile using shapefile.py and my database has records more that 65k which is the limit of the shapefile. How can I create shapefiles then?

Is it possible that somehow I am able to creat shapefile and store them directly into the PostGIS using shapefile.py?

Best Answer

Theoretically, a shapefile can contain about 70 million point features. The size of either the .shp or .dbf files must not exceed 2GB.

Also, why bother creating a shapefile to insert to PostGIS? Try directly inserting the data using something like psycopg2, which makes the whole import process simpler, faster, and more direct.


Here are a few short examples to directly insert to PostGIS. First with simple point data, then with more complicated polygon data (a similar geometry construction and WKB export can also be done with OGR).

import psycopg2
from random import random
from shapely.geometry import Polygon

# Make a connection https://www.psycopg.org/docs/module.html
conn = psycopg2.connect(dbname='mydb')
curs = conn.cursor()

# Insert Point data into mypts
curs.execute('''\
    create table if not exists mypts(gid serial primary key, geom geometry(Point,4326));
''')
# Some longitude/latitude points (in that order!)
lon_lat = [(random(), random()) for x in range(100)]
curs.executemany('''\
    insert into mypts(geom) values(ST_SetSRID(ST_MakePoint(%s, %s), 4326));
''', lon_lat)
conn.commit()

# Insert Polygon data into mypoly
curs.execute('create table if not exists mypoly(gid serial primary key, geom geometry(Polygon,4326))')
# Use Shapely to piece together exterior linear ring, and interior rings with lists of coordinates
poly = Polygon(
            shell=[(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
            holes=[[(2, 2), (2, 6), (7, 7), (2, 2)]])
curs.execute('''\
    insert into mypoly(geom) values(ST_SetSRID(%s::geometry, 4326));
''', (poly.wkb_hex,))
conn.commit()