using python mapscript to create a shapefile and dbf
i always have trouble remembering how to use mapscript. it's pretty simple, but the docs are hard to find and the test cases (though excellent!) have a lot of abstraction.
heres some code that creates a shapefile and dbf (using another module). and does a quick projection at the start.
heres some code that creates a shapefile and dbf (using another module). and does a quick projection at the start.
import mapscript as M
import random
from dbfpy import dbf
#########################################
# do some projection
#########################################
p = 'POINT(466666 466000)'
shape = M.shapeObj.fromWKT(p)
projInObj = M.projectionObj("init=epsg:32619")
projOutObj = M.projectionObj("init=epsg:4326")
shape.project(projInObj, projOutObj)
print shape.toWKT()
#########################################
# create a shapefile from scractch
#########################################
ms_dbf = dbf.Dbf("/tmp/t.dbf", new=True)
ms_dbf.addField(('some_field', "C", 10))
ms_shapefile = M.shapefileObj('/tmp/t.shp', M.MS_SHAPEFILE_POLYGON)
for i in xrange(10):
ms_shape = M.shapeObj(M.MS_SHAPE_POLYGON)
ms_line = M.lineObj()
for j in xrange(10):
ms_line.add(M.pointObj(random.randint(0,99), -random.randint(0,99)))
ms_shape.add(ms_line)
ms_shapefile.add(ms_shape)
rec = ms_dbf.newRecord()
rec['some_field'] = 'hi' + str(i)
rec.store()
ms_dbf.close()
Comments