def run(): sf = shapelib.open( os.getenv("SWFP_DATADIR") + "/mapnik_render/world_boundaries/world_boundaries_m.shp") d = dbflib.DBFFile( os.getenv("SWFP_DATADIR") + "/mapnik_render/world_boundaries/world_boundaries_m.dbf") num_shapes = sf.info()[0] assert num_shapes == d.record_count() swedish_polygons = 0 for idx in xrange(num_shapes): obj = sf.read_object(idx) rec = d.read_record(idx) if rec['CNTRY_NAME'] == 'Sweden': #print "Sweden: ",obj.vertices() swedish_polygons += 1 assert len(obj.vertices()) == 1 out = [] for vert in obj.vertices()[0]: cd = prj.inverse(mapnik.Coord(vert[1], vert[0])) #print "lat: %s, lon: %s,"%(cd.y,cd.x) out.append(mapper.format_lfv(cd.y, cd.x)) print "Swedpol:", " - ".join(out) print "Swedish polygons: %d" % (swedish_polygons, )
def shpGetVertices(shpFile, keyName=None): """ Returns a dictionary of arrays containing coordinate pairs representing vertices contained in the shapefile. Dictionary keys can either be a field contained within the corresponding dbf file (through the optional kwarg keyName), or if no key name is provided, the object id number (i.e. the record number) is used. WARNING: If any records share the same value for the chosen key, then only one record will be retained in the returned values. As yet untested on MULTIPOINT shapefiles (polylines, polygons). Input: shpFile - path to a shapefile, excluding the extension (shapelib/dbflib automatically append the correct extension) keyName - (optional) name of a field in the dbf file that acts as a key for the dictionary of returned vertices Output: dictionary keyed by object id number or optionally a field name, with values being arrays of vertices of the corresponding shape object. Example: vertices = shpGetVertices(shpFile,keyName) """ try: shpfh = shapelib.open(shpFile) except IOError: logger.warn("Cannot open %s"%shpFile) raise try: dbffh = dbflib.open(shpFile) except IOError: logger.warn("Cannot open %s.dbf"%shpFile) raise vertices = {} nshapes = shpfh.info()[0] nfields = dbffh.field_count() for oid in xrange(nshapes): shpdata = shpfh.read_object(oid) dbfdata = dbffh.read_record(oid) nparts = len(shpdata.vertices()) v = [] for p in xrange(nparts): v.append(shpdata.vertices()[p]) if keyName and dbfdata.has_key(keyName): vertices[dbfdata[keyName]] = v else: vertices[oid] = v shpfh.close() dbffh.close() return vertices
def packReceiverToPKL(receiverShapeFile, IDFieldName): shp = shapelib.open(receiverShapeFile) dbf = dbflib.open(receiverShapeFile) receiverObjects = [] for r in xrange(shp.info()[0]): shpObj = shp.read_object(r) vtx = shpObj.vertices()[0] identify = dbf.read_record(r)[IDFieldName] rObj = Receivers(vtx, identify) receiverObjects.append(rObj) shp.close() dbf.close() return receiverObjects
def shpType(shpFile): """ Return the type of objects contained in the shapefile as a string """ try: shpfh = shapelib.open(shpFile) except IOError: logger.warn("Cannot open %s"%shpFile) raise shp_type = shapelib.type_name(shpfh.info()[1]) shpfh.close() return shp_type
def load(self): shapes = shapelib.open(self.name) if len(self._fields) != 0: data = dbflib.open(self.name) list = SpatialPointList() for i in range(shapes.info()[0]): records = Dictionary() for key, gen in self._fields: records.update([(gen, float(data.read_record(i)[key]))]) x = float(shapes.read_object(i).vertices()[0][0]) y = float(shapes.read_object(i).vertices()[0][1]) list.append(SpatialPoint(x, y, records, self)) return list
def read_shapefile(fp, heal=True): """ Reads an ESRI Shapeifle and converts it to Shapely Geometry objects. The DBF is also read and each returned geometry has its DBF attributes attached. Examples: points = read_shapefile("airports.shp") p0 = points[0] p0.geom_type >>'Point' p0['Airport_code'] >>'YYZ' Args: fp (str or unicode): The path to the shapefile heal (bool): If true, this function will attempt to auto-heal any invalid the geometry using buffer(0). Returns: List of ShapelyGeometry objects """ basepath, _ = path.splitext(fp) shp_path = basepath + ".shp" dbf_path = basepath + ".dbf" shapefile = shp.open(shp_path) try: shp_length, shp_type, x_extents, y_extents = shapefile.info() tablefile = dbf.open(dbf_path) except: shapefile.close() raise try: geoms = [] assert shp_length == tablefile.record_count() for fid in xrange(shp_length): record = shapefile.read_object(fid) geom = _make_geom(record) if not geom.is_valid and heal: geom = geom.buffer(0) attrs = tablefile.read_record(fid) geoms.append(ShapelyGeometry(geom, attrs)) return geoms finally: shapefile.close() tablefile.close()
def pointsFromSHP(pointsFilename, datafield=[]): shapes = shapelib.open(pointsFilename) if len(datafield) != 0: data = dbflib.open(pointsFilename) list = SpatialPointList() for i in range(shapes.info()[0]): records = {} pair = [] for entry in datafield: pair.append((entry, data.read_record(i)[entry])) records.update(pair) x = shapes.read_object(i).vertices()[0][0] y = shapes.read_object(i).vertices()[0][1] list.append(SpatialPoint(x, y, records)) return list
def packReceiverToPKL(receiverShapeFile, IDFieldName): shp = shapelib.open(receiverShapeFile) dbf = dbflib.open(receiverShapeFile) receiverObjects = [] for r in xrange(shp.info()[0]): print "receiver: ", r shpObj = shp.read_object(r) vtx = shpObj.vertices()[0] absoluteHeight = antw_dtm.read_vtx(vtx) identify = dbf.read_record(r)[IDFieldName] rObj = Receivers(vtx, identify, absoluteHeight) receiverObjects.append(rObj) shp.close() dbf.close() return receiverObjects
def pointsFromSHP (pointsFilename, datafield=[]): shapes = shapelib.open(pointsFilename) if len(datafield) != 0: data = dbflib.open (pointsFilename) list = SpatialPointList () for i in range (shapes.info ()[0]): records = {} pair = [] for entry in datafield: pair.append ((entry, data.read_record (i)[entry])) records.update (pair) x = shapes.read_object (i).vertices ()[0][0] y = shapes.read_object (i).vertices ()[0][1] list.append (SpatialPoint (x, y, records)) return list
def load(self): shapes = shapelib.open(self.name) data = dbflib.open(self.name) polys = PolygonDictionary() for i in range(shapes.info()[0]): shp = shapes.read_object(i) d = data.read_record(i)[self._primary] if self._subset != None: for item in self._subset: if item == d: p = SpatialPolygon(shp.vertices(), [], self) polys.update({d: p}) else: p = SpatialPolygon(shp.vertices(), [], self) polys.update({d: p}) return polys
def packSourceToPKL(sourceShapeFile): shp = shapelib.open(sourceShapeFile) dbf = dbflib.open(sourceShapeFile) specField = ['L_63', 'L_125', 'L_250', 'L_500', 'L_1000', 'L_2000', 'L_4000', 'L_8000'] sourceObjects = [] for r in xrange(shp.info()[0]): shpObj = shp.read_object(r) p_source = shpObj.vertices()[0] # (x, y) rec = dbf.read_record(r) specD = [rec[f] for f in specField] spec = [specD] sObj = Sources(p_source, spec) sourceObjects.append(sObj) shp.close() dbf.close() return sourceObjects # list of soruce object. values defined in "Sources" can be queried
def polysFromSHP (polysFilename, datafield, polySubset): R.importLibrary ('sp') shapes = shapelib.open(polysFilename) data = dbflib.open (polysFilename) polyList = [] for i in range (shapes.info ()[0]): shp = shapes.read_object (i) d = data.read_record (i)[datafield] if len(polySubset) != 0: for item in polySubset: if item == d: p = SpatialPolygon (i, shp.vertices (), d) polyList.append(p) else: p = SpatialPolygon (i, shp.vertices (), d) polyList.append(p) return polyList
def polysFromSHP(polysFilename, datafield, polySubset): R.importLibrary('sp') shapes = shapelib.open(polysFilename) data = dbflib.open(polysFilename) polyList = [] for i in range(shapes.info()[0]): shp = shapes.read_object(i) d = data.read_record(i)[datafield] if len(polySubset) != 0: for item in polySubset: if item == d: p = SpatialPolygon(i, shp.vertices(), d) polyList.append(p) else: p = SpatialPolygon(i, shp.vertices(), d) polyList.append(p) return polyList
def packSourceToPKL(sourceShapeFile): shp = shapelib.open(sourceShapeFile) dbf = dbflib.open(sourceShapeFile) specField = [ 'L_63', 'L_125', 'L_250', 'L_500', 'L_1000', 'L_2000', 'L_4000', 'L_8000' ] sourceObjects = [] for r in xrange(shp.info()[0]): shpObj = shp.read_object(r) p_source = shpObj.vertices()[0] # (x, y) rec = dbf.read_record(r) specD = [rec[f] for f in specField] spec = [specD] sObj = Sources(p_source, spec) sourceObjects.append(sObj) shp.close() dbf.close() return sourceObjects # list of soruce object. values defined in "Sources" can be queried
def doit(gts, i): shp = shapelib.open('squaw.shp', 'r') dbf = dbflib.DBFFile('squaw.dbf', 'r') shape = numpy.array( shp.read_object(0).vertices()[0] ) # We have our outline lats = shape[:,1] lons = shape[:,0] # load up data dlons = numpy.arange(-110., -89.99, 0.01) dlats = numpy.arange(55.0, 39.99, -0.01) nc = netCDF3.Dataset(make_fp(gts)) val = nc.variables["preciprate_hsr"][:] cfg = { 'mpMinLonF' : min(lons), 'mpMaxLonF' : max(lons), 'mpMinLatF' : min(lats), 'mpMaxLatF' : max(lats), '_drawx' : [lons], '_drawy' : [lats], 'cnFillMode' : 'RasterFill', 'cnLevelSelectionMode': "ExplicitLevels", 'cnLevels' : [0.01,0.05,0.1,0.25,0.5,0.75,1,1.25,1.5,2.0,2.5,3,4], 'wkColorMap': 'BlAqGrYeOrRe', 'nglSpreadColorStart': -1, 'nglSpreadColorEnd' : 2, '_MaskZero' : True, 'lbTitleString' : "[inch/hr]", '_valid' : 'Valid %s' % ( gts.localtime().strftime("%d %B %Y %I:%M %p %Z"),), '_title' : "NMQ Q2 Precipitation Rate over Squaw River Basin [inch/hr]", } # Scale factor is 10 tmpfp = iemplot.simple_grid_fill(dlons, dlats, val / 254.0, cfg) fn = "frames/%05d.png" % (i,) iemplot.postprocess(tmpfp, '', fname=fn) nc.close()
def run(): sf=shapelib.open(os.getenv("SWFP_DATADIR")+"/mapnik_render/world_boundaries/world_boundaries_m.shp") d=dbflib.DBFFile(os.getenv("SWFP_DATADIR")+"/mapnik_render/world_boundaries/world_boundaries_m.dbf") num_shapes=sf.info()[0] assert num_shapes==d.record_count() swedish_polygons=0 for idx in xrange(num_shapes): obj=sf.read_object(idx) rec=d.read_record(idx) if rec['CNTRY_NAME']=='Sweden': #print "Sweden: ",obj.vertices() swedish_polygons+=1 assert len(obj.vertices())==1 out=[] for vert in obj.vertices()[0]: cd=prj.inverse(mapnik.Coord(vert[1],vert[0])) #print "lat: %s, lon: %s,"%(cd.y,cd.x) out.append(mapper.format_lfv(cd.y,cd.x)) print "Swedpol:", " - ".join(out) print "Swedish polygons: %d"%(swedish_polygons,)
if not os.path.exists(filename): sys.stderr.write("file '%s' not found\n" % filename) sys.exit(0) f = open(filename, "r") #filename = "./vg2500_bld.dbf" #filename = "./vg2500_sta.dbf" #filename = "./vg2500_rbz.dbf" #filename = "./vg2500_krs.dbf" # for europe.dbf: set fac to 1.0 and comment out call to simplify_points fac = 100000.0 if len(sys.argv) > 2: fac = float(sys.argv[2]) f = shp.open(filename) nParts = f.info()[0] ''' extract bounding box ''' minx = f.info()[2][0] / fac miny = f.info()[2][1] / fac maxx = f.info()[3][0] / fac maxy = f.info()[3][1] / fac print "bbox = [%0.2f,%0.2f,%0.2f,%0.2f];" % (minx * 0.99, maxy * 1.01, maxx * 1.01, miny * 0.99) ''' Read the paths information ''' print "paths = [];"
def __init__(self,Cvsq, Ctsq, buildingFile, receiverFile, sourceFile, resultOutFile,\ rZoneSize=2000.0, r2sDist=1500.0, flags=['D', 'E', 'N'], modelType='scattering'): ''' buildingFile, receiverFile, sourceFile and resultOutFile are shape file names resultOutFile is also the new folder receiverZone is the vertix of the smaller receiver region SBzone is the corresponding zone of the receivers flags -> 'D', 'E', 'N' represent for day, evening and Night ''' self.Cvsq = Cvsq self.Ctsq = Ctsq self.i = 0 # acounter to write results out print 'initialing...' # common constants self.fr = [63, 125, 250, 500, 1000, 2000, 4000, 8000] self.waveLength = 340.0/np.array(self.fr) self.Aweight = np.array([-26.2, -16.1,-8.6, -3.2, 0, 1.2, 1, -1.1]) # {63:-26.2, 125:-16.1, 250:-8.6, 500:-3.2, 1000:0, 2000:1.2, 4000:1, 8000:-1.1} self.sHi = 0.0 # source Height♠ self.rHi = 4.5 # receiver height self.modelType = modelType # preparing to write results out print "preparing to write to file: ", resultOutFile if not os.path.exists(resultOutFile): os.mkdir(resultOutFile) shutil.copy(receiverFile+'.shp', resultOutFile) shutil.copy(receiverFile+'.shx', resultOutFile) self.DBFOut = dbflib.create(resultOutFile+'\\'+resultOutFile) self.DBFOut.add_field("GENTID", dbflib.FTInteger, 7, 0) # add the idential ID self.fieldName = ['L_63', 'L_125', 'L_250', 'L_500', 'L_1000', 'L_2000', 'L_4000', 'L_8000', 'LA'] for fg in flags: for f in self.fieldName: # add new field self.DBFOut.add_field(f+fg, dbflib.FTDouble, 9, 1) # write log print 'Create log' yr = time.gmtime()[0] mon = time.gmtime()[1] day = time.gmtime()[2] hour = time.gmtime()[3] minu = time.gmtime()[4] label = str(yr)+str(mon)+str(day)+str(hour)+str(minu) logw = open(resultOutFile+'\\'+'log_' + label +'.txt', 'w') logw.write(time.ctime()+'\r\n') logw.write('buildingFile: '+buildingFile+'\r\n'+'receiverFile: '+receiverFile+'\r\n') logw.write('sourceFiel: '+sourceFile+'\r\n') logw.write('Dimension of receiver zone: '+str(rZoneSize)+'*'+str(rZoneSize)+'\r\n') logw.write('Maximum distance from source to receiver: '+str(r2sDist)+'\r\n') logw.write('Source type: ' + str(flags)+'\r\n') logw.write('Model type: ' + modelType + '\r\n\r\n') tic = time.clock() print 'Prepare source, receiver and buildings' # try: if not os.path.exists('pkData'): os.mkdir('pkData') if not os.path.exists('pkData\\PKLsourceOBJ.pkl'): sourceObjects = packSourceToPKL(sourceFile) sw = open('pkData\\PKLsourceOBJ.pkl', 'wb') pickle.dump(sourceObjects, sw, 2) # protocal 2 for verion 2.x, 3for 3.x sw.close() else: sr = open('pkData\\PKLsourceOBJ.pkl', 'rb') sourceObjects = pickle.load(sr) sr.close() if not os.path.exists('pkData\\PKLreceiverOBJ.pkl'): receiverObjects = packReceiverToPKL(receiverFile, 'GID') rw = open('pkData\\PKLreceiverOBJ.pkl', 'wb') pickle.dump(receiverObjects, rw, 2) rw.close() else: rr = open('pkData\\PKLreceiverOBJ.pkl', 'rb') receiverObjects = pickle.load(rr) rr.close() if not os.path.exists('pkData\\PKLbuildingOBJ.pkl'): polygonObjects = packBuildingToPKL(buildingFile) bw = open('pkData\\PKLbuildingOBJ.pkl', 'wb') pickle.dump(polygonObjects, bw, 2) bw.close() else: br = open('pkData\\PKLbuildingOBJ.pkl', 'rb') polygonObjects = pickle.load(br) br.close() toc1 = time.clock() logw.write('Initializing takes '+str(toc1-tic)+' seconds\r\n') print 'calculating...' # test the zones rSHP = shapelib.open(receiverFile) if rSHP.info()[3][0]-rSHP.info()[2][0]>9999999999 or rSHP.info()[3][1]-rSHP.info()[2][1]>999999999: smObj = SmallerZones(receiverFile, rZoneSize, r2sDist) [pxList, pyList] = smObj._generateGridVertices() [rZones, sbZones] = smObj._verticesToZones(pxList, pyList) print 'Divid to ', len(rZones), ' zones' for n in xrange(len(rZones)): print 'Calculating zone ', n rz = rZones[n] sbz = sbZones[n] # shrinking region or load shape self.receiverObjects = shrinkReceiverZone(receiverObjects,rz) if len(self.receiverObjects)>0: listPolygonObjects = shrinkBuildingZone(polygonObjects, sbz) self.buildings = KDTreeManualPolygon(listPolygonObjects) self.sourceObjects = shrinkSourceZone(sourceObjects, sbz) self.runModel() else: self.buildings = KDTreeManualPolygon(polygonObjects) self.sourceObjects = sourceObjects self.receiverObjects = receiverObjects self.runModel() # except: # logw.write('\r\n\r\nAborted unexpectedly!!! \r\n\r\n') toc2 = time.clock() logw.write('Calculating takes '+str(toc2-tic)+' seconds\r\n') logw.close()
def __init__(self, receiverShape, sizeReceiverZone=2000.0, dist2outerBuilding=1500.0): self.dRZone = sizeReceiverZone self.d2B = dist2outerBuilding self.Rshp = shapelib.open(receiverShape) self.RB = [self.Rshp.info()[2], self.Rshp.info()[3]] # [(x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max)] self.Rshp.close()
if not os.path.exists(filename): sys.stderr.write("file '%s' not found\n" % filename) sys.exit(0) f = open(filename, "r") #filename = "./vg2500_bld.dbf" #filename = "./vg2500_sta.dbf" #filename = "./vg2500_rbz.dbf" #filename = "./vg2500_krs.dbf" # for europe.dbf: set fac to 1.0 and comment out call to simplify_points fac = 100000.0 if len(sys.argv)>2: fac = float(sys.argv[2]) f = shp.open(filename) nParts = f.info()[0] ''' extract bounding box ''' minx = f.info()[2][0]/fac miny = f.info()[2][1]/fac maxx = f.info()[3][0]/fac maxy = f.info()[3][1]/fac print "bbox = [%0.2f,%0.2f,%0.2f,%0.2f];" %(minx*0.99,maxy*1.01,maxx*1.01,miny*0.99) ''' Read the paths information '''
if len(sys.argv)<2: sys.stderr.write("call: python shp2jsx.py filename [factor] \n") sys.stderr.write("\t file name has to have the ending dbf. \n") sys.exit(0) filename = sys.argv[1] if not os.path.exists(filename): sys.stderr.write("file '%s' not found\n" % filename) sys.exit(0) f = open(filename, "r") fac = 1.0 #100000.0 if len(sys.argv)>2: fac = float(sys.argv[2]) f = shp.open(filename) # get handle to the the shape file nParts = f.info()[0] ''' extract bounding box ''' minx = f.info()[2][0]/fac miny = f.info()[2][1]/fac maxx = f.info()[3][0]/fac maxy = f.info()[3][1]/fac print "bbox = [%0.2f,%0.2f,%0.2f,%0.2f];" %(minx*0.99,maxy*1.01,maxx*1.01,miny*0.99) ''' Read the paths information '''