def shp2geojson(inFilename,outFileName,clip_rect=None, srs=None): # read the shapefile reader = shapefile.Reader(inFilename) fields = reader.fields[1:] fieldNames = [field[0] for field in fields] #print 'fieldNames', fieldNames features = [] skipped_no_points = 0 for (sr, ss) in itertools.izip(reader.iterRecords(), reader.iterShapes()): if len(ss.points) == 0: skipped_no_points += 1 continue atr = dict(zip(fieldNames, sr)) geom = ss.__geo_interface__ if not clip_rect or geofeatures.coordinates_intersect_rect_q(geom['coordinates'],clip_rect): #print 'DEB geom:', geom #features.append(dict(type='Feature', geometry=geom, properties=atr)) features.append(geojson.Feature(geometry=geom, properties=atr)) # log messages if skipped_no_points > 0: log.warning("Skipped %d shapes in %s because they have no geometry.", skipped_no_points, inFilename) # write the geojson file geofeatures.write_geojson(features, outFileName, srs=srs)
def write_geojson(self, name=None): if not name: name=self.name # using geo_interface for self is memory inefficient: requires gen of geojson for entire pnwk #geofeatures.write_geojson(self, name+self.FILE_EXTENSION, srs=self.srs) geofeatures.write_geojson( self.segs.values()+self.jcts.values(), name+self.FILE_EXTENSION, srs=self.srs)
def write_geojson(self, name): geofeatures.write_geojson(self,name+OSM_GEOJSON_FILE_EXTENSION)