def runTiler(shpfile,outdir,config,zoom=None): if not os.path.exists(shpfile): raise StandardError('Shape File: %s, does not exist!'%shpfile) if not os.path.exists(outdir): os.mkdir(outdir) shapePath, shapeName = os.path.split(shpfile) shapeName = shapeName.split('.')[0] #c = Classifier(shapePath,shapeName) #c.CheckUID(config['IdName']) #mapProj = ms.projectionObj(config['projString']) shpfile = ms.shapefileObj(shpfile,-1) # "value of -1 to open an existing shapefile"-ms.docs bounds = shpfile.bounds return bounds bounds.project(mapProj,GMERC) mapobj = ms.fromstring(MAPFILE%{'shapePath':shapePath,'shapeName':shapeName,'classes':c(),'projString':config['projString']}) mapobj.extent = bounds mapobj.draw().save('f.png') t = Tiler(mapobj,outdir,zoom) print "extents: " bounds.project(GMERC,WGS) print (bounds.maxx + bounds.minx) / 2 print (bounds.maxy + bounds.miny) / 2 return c,t
def testGetDBFInfo(self): """Fetch dbf information from shapefile""" pth = os.path.join(TESTS_PATH, "polygon.shp") sf = mapscript.shapefileObj(pth) assert sf.getDBF() is not None, sf.getDBF() assert sf.getDBF().nFields == 2, sf.getDBF().nFields assert sf.getDBF().getFieldName(0) == 'FID', sf.getDBF().getFieldName(0) assert sf.getDBF().getFieldName(1) == 'FNAME', sf.getDBF().getFieldName(1)
def get_shapefile_object(sf_path): # make sure can access .shp file, create shapefileObj if os.access(sf_path, os.F_OK): sf_obj = mapscript.shapefileObj(sf_path) else: print("Can't access {}".format(sf_path)) sys.exit(2) return sf_obj
def save_to_mapfile(self,out_path): path = self.__pathToShapeFile shapePath, shapeName = os.path.split(path) shapeName = shapeName.split('.')[0] shpfile = ms.shapefileObj(path,-1) # "value of -1 to open an existing shapefile"-ms.docs #print self.projection src_prj = ms.projectionObj(self.projection) shpfile.bounds.project(src_prj,self.targetProj) mapobj = ms.fromstring(MAPFILE%{'shapePath':shapePath,'shapeName':shapeName,'color':'[dtmValue]','projString':self.projection,'size':self.size}) mapobj.extent = shpfile.bounds mapobj.save(out_path)
def __getMapObj(self): path = self.__pathToShapeFile shapePath, shapeName = os.path.split(path) shapeName = shapeName.split('.')[0] shpfile = ms.shapefileObj(path,-1) # "value of -1 to open an existing shapefile"-ms.docs #print self.projection src_prj = ms.projectionObj(self.projection) shpfile.bounds.project(src_prj,self.targetProj) if not self.idVar: mapobj = ms.fromstring(MAPFILE%{'shapePath':shapePath,'shapeName':shapeName,'color':DEFAULT_COLOR,'projString':self.projection,'size':self.size}) else: mapobj = ms.fromstring(MAPFILE%{'shapePath':self.__workSpace,'shapeName':'dtm','color':'[dtmValue]','projString':self.projection,'size':self.size}) mapobj.extent = shpfile.bounds return mapobj
def shp_info(shp, shx): """ Returns details about shapefile Aguments: shp: Binary contents of the .shp file. shx: Binary contents of the .shx file. """ shpf = tempfile.NamedTemporaryFile("wb", suffix=".shp", delete=False) shpf.write(shp) shpf.close() shxf = open(shpf.name[:-3] + "shx", "wb") shxf.write(shx) shxf.close() shp = pysal.open(shpf.name, "r") n = len(shp) shp.close() # Mapscript requires a DBF to read a shapefile. dbf = pysal.open(shpf.name[:-3] + "dbf", "w") dbf.header = ["dtmValue"] dbf.field_spec = [("C", 7, 0)] for i in range(n): dbf.write(["#%0.6X" % (i + ID_OFFSET)]) dbf.close() shp = mapscript.shapefileObj(shpf.name) n = shp.numshapes x = shp.bounds.minx y = shp.bounds.miny X = shp.bounds.maxx Y = shp.bounds.maxy # wgs84 = mapscript.projectionObj('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ') # gmerc = mapscript.projectionObj('init=epsg:3857') shp.bounds.project(WGS84, GMERC) gx = shp.bounds.minx gy = shp.bounds.miny GX = shp.bounds.maxx GY = shp.bounds.maxy os.remove(shpf.name) os.remove(shxf.name) return (n, [x, y, X, Y], [gx, gy, GX, GY])
import sys import os # Utility functions. def usage(): """ Display usage if program is used incorrectly. """ print "Syntax: %s base_filename" % sys.argv[0] sys.exit(2) # Make sure passing in filename argument. if len(sys.argv) != 2: usage() # Make sure can access .shp file, create shapefileObj. if os.access(sys.argv[1] + ".shp", os.F_OK): sf_obj = mapscript.shapefileObj(sys.argv[1], -1) else: print "Can't access shapefile" sys.exit(2) # Dictionary of shapefile types. types = { 1: 'point', 3: 'arc', 5: 'polygon', 8: 'multipoint' } # Print out basic information that is part of the shapefile object. print "Shapefile %s:" % sys.argv[1] print print "\ttype: %s" % types[sf_obj.type] print "\tnumber of features: %i" % sf_obj.numshapes
def testAddEmpty(self): """expect an error rather than segfault when adding an empty shape""" # See bug 1201 sf = mapscript.shapefileObj('testAddDud.shp', 1) so = mapscript.shapeObj(mapscript.MS_SHAPE_POINT) self.assertRaises(mapscript.MapServerError, sf.add, so)
# geocoder base request URL sGeocoderUrl = "http://www.geocoder.ca/?geoit=xml&locate=" # set csv record indices sAddress = int(sys.argv[2]) sCity = int(sys.argv[3]) sStateProv = int(sys.argv[4]) # open file fCsv = open(sys.argv[1], 'r') # read csv csvIn = csv.reader(fCsv) # create output shp/shx msSFOut = mapscript.shapefileObj(sys.argv[-1], 1) # create output dbf dbfOut = Dbf(sys.argv[-1]+".dbf", new=True) # add fields dbfOut.addField( ("address", 'C', 255), ("city", 'C', 255), ("stateprov", 'C', 255), ("x", 'N', 6,2), ("y", 'N', 7,2) ) for aRow in csvIn: # concatenate request params
""" import mapscript import sys import os import subprocess def usage(): print "USAGE: %s filename" % sys.argv[0] sys.exit(1) if len(sys.argv) != 2: usage() if os.access(sys.argv[1] + ".shp", os.F_OK): shp = mapscript.shapefileObj(sys.argv[1], -1) if not os.access(sys.argv[1] + ".dbf", os.F_OK): print "Error accessing associated dbf?" sys.exit(2) dbf = sys.argv[1] + ".dbf" elif os.access(sys.argv[1][:-4] + ".shp", os.F_OK): shp = mapscript.shapefileObj(sys.argv[1][:-4] + ".shp", -1) if not os.access(sys.argv[1][:-4] + ".dbf", F_OK): print "Error accessing associated dbf?" sys.exit(2) dbf = sys.argv[1][:-4] + ".dbf" else: print "Error accessing shapefile \"" + sys.argv[1] + "\"" shape = mapscript.shapeObj(-1) ofile = open("geoidx.db", "w")
# Combine DM layers into one shapefile! # Daryl Herzmann 4 Nov 2005 import mapscript, dbflib, sys, os ts = sys.argv[1] outshp = mapscript.shapefileObj('dm_%s.shp' % ts, mapscript.MS_SHAPEFILE_POLYGON) dbf = dbflib.create("dm_%s" % ts) dbf.add_field("DCAT", dbflib.FTInteger, 1, 0) counter = 0 for d in range(5): if not os.path.isfile("Drought_Areas_US_D%s.shp" % d): print "No Shapefile for D %s" % (d, ) continue shp = mapscript.shapefileObj('Drought_Areas_US_D%s.shp' % d) for i in range(shp.numshapes): shpObj = shp.getShape(i) outshp.add(shpObj) dbf.write_record(counter, [ d, ]) del shpObj counter += 1 del outshp del dbf
# Combine DM layers into one shapefile! # Daryl Herzmann 4 Nov 2005 import mapscript, dbflib, sys, os ts = sys.argv[1] outshp = mapscript.shapefileObj('dm_%s.shp'%ts, mapscript.MS_SHAPEFILE_POLYGON ) dbf = dbflib.create("dm_%s"%ts) dbf.add_field("DCAT", dbflib.FTInteger, 1, 0) counter = 0 for d in range(5): if not os.path.isfile("Drought_Areas_US_D%s.shp" %d): print "No Shapefile for D %s" % (d,) continue shp = mapscript.shapefileObj('Drought_Areas_US_D%s.shp' %d) for i in range( shp.numshapes ): shpObj = shp.getShape(i) outshp.add( shpObj ) dbf.write_record(counter, [d,]) del shpObj counter += 1 del outshp del dbf
#!/usr/bin/env python # http://old-mapserver.gis.umn.edu/doc40/howto-mapscript-python_2.html # http://trac.osgeo.org/mapserver/browser/trunk/mapserver/mapscript/ruby/examples/shp2img.rb import mapscript shapepath = '../../data/' shapename = 'world_borders.shp' shp = mapscript.shapefileObj(shapepath+shapename,-1) m = mapscript.mapObj('') m.shapepath = shapepath m.height = 400 m.width = 600 m.extent = shp.bounds shapetypes = { mapscript.MS_SHAPEFILE_POINT:mapscript.MS_LAYER_POINT, mapscript.MS_SHAPEFILE_ARC:mapscript.MS_LAYER_LINE, mapscript.MS_SHAPEFILE_POLYGON:mapscript.MS_LAYER_POLYGON, mapscript.MS_SHAPEFILE_MULTIPOINT:mapscript.MS_LAYER_LINE } layer = mapscript.layerObj(m) layer.name = shapename layer.type = shapetypes[shp.type] layer.status = mapscript.MS_ON layer.data = shapename
import mapscript import sys import os import subprocess def usage(): print "USAGE: %s filename" % sys.argv[0] sys.exit(1) if len(sys.argv) != 2: usage() if os.access(sys.argv[1] + ".shp", os.F_OK): shp = mapscript.shapefileObj(sys.argv[1], -1) if not os.access(sys.argv[1] + ".dbf", os.F_OK): print "Error accessing associated dbf?" sys.exit(2) dbf = sys.argv[1] + ".dbf" elif os.access(sys.argv[1][:-4] + ".shp", os.F_OK): shp = mapscript.shapefileObj(sys.argv[1][:-4] + ".shp", -1) if not os.access(sys.argv[1][:-4] + ".dbf", F_OK): print "Error accessing associated dbf?" sys.exit(2) dbf = sys.argv[1][:-4] + ".dbf" else: print "Error accessing shapefile \"" + sys.argv[1] + "\"" shape = mapscript.shapeObj(-1) ofile = open("geoidx.db", "w")