Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

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
import random
from shapely.geometry import Polygon
from binascii import hexlify

# 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(), 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)]])
poly_wkb = hexlify(poly.wkb).upper() # convert to ASCII version of WKB
curs.execute('insert into mypoly(geom) values(ST_SetSRID(%s::geometry, 4326));', (poly_wkb,))
conn.commit()
share|improve this answer
2  
+1 for don't bother with Shapefiles if you can have PostGIS. –  underdark Jul 7 '12 at 11:15
    
Thanks for the reply @Mike, but the problem is like this, I already have the attribute data in MySql and now we are planning to move to Postgres and use PostGIS. So my script which uses shapefile.py is extracting info from the raster and lat/lon from MySql db and then creating shapefile. So I need to add this directly to PostGIS and then view it using Geoserver. So how can I do that? –  Sam007 Jul 16 '12 at 16:25
    
@Sam007 not sure what you are asking exactly; you might need to ask a separate question with much more detail. Generally, try ogr2ogr to transfer spatial data between databases. If that is insufficient, there are many different ways to build a python script to directly insert data. –  Mike T Jul 16 '12 at 18:46
    
would u know of any sample code for adding the data by code to PostGIS database directly? –  Sam007 Jul 16 '12 at 19:28
    
@Sam007 there are several methods, but it depends on the type of geometries. For instance, inserting point data is simpler than inserting polygon data. The former I'd use ST_MakePoint, while the later, I'd construct the linear rings with OGR or Shapely, then generate the WKB to insert. –  Mike T Jul 16 '12 at 19:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.