[Mapserver-users] Creating dynamic shapefiles with Python Mapscript

Michael Schulz mschulz at webgis.de
Tue Nov 18 06:49:40 EST 2003


Hi Bjoern,
if you only want to create a point data shapefile, you don't need to 
create a shapeObj/lineObj.
You could just do:

newshpfile = mapscript.shapefileObj(shpName,mapscript.MS_SHAPEFILE_POINT)
# iterate over your result set
for l in r[1]:
  p = mapscript.pointObj()
  p.x = l[0]
  p.y = l[1]
  newshpfile.addPoint(p)
...

If you want to create a line shapefile:

newlshpfile = mapscript.shapefileObj(shpName,mapscript.MS_SHAPEFILE_ARC)
lineobjekt = mapscript.lineObj()
shpobjekt = mapscript.shapeObj(mapscript.MS_SHAPE_LINE)
for l in r[1]:
  p = mapscript.pointObj()
  p.x = l[0]
  p.y = l[1]
  lineobjekt.add(p)
shpobjekt.add(lineobjekt)
newlshpfile.add(shpobjekt)

For a queryable shapefile you still need the corresponding dbf-file. I suggest using the python binding for dbflib. Don't go for the mapscript dbf functions. 

HTH, Michael

-----------------------------------------------------------
Michael Schulz                                in medias res
Dipl.-Geologe                              Gesellschaft für
                                Informationstechnologie mbH
                                     In den Weihermatten 66
                                             79108 Freiburg
                                     0761 55695-95 (Fax 96)
mschulz at webgis.de              www.webgis.de/www.zopecms.de




Bjoern Platzen schrieb:

>        myShape = mapscript.shapefileObj(shpName, 
>mapscript.MS_SHAPEFILE_POINT)
>
>        myLayer = self.mymap.getLayerByName(layer)
>	#open the sql-file
>        query = open('sql/'+queryfile+'.sql', 'r').read()
>		#querying the database
>        r = self.Conn.get_result(query)
>        if r[0]>0:#r=(numrecords,[[k1,k2],[k3,k4],[k5,k6],...]
>	    #define a new shape object
>            newShape = mapscript.shapeObj(mapscript.MS_SHAPE_POINT)
>            for l in r[1]:
>		#define a lineobject
>                part = mapscript.lineObj()
>                #make the pointobject
>		newPoint = mapscript.pointObj()
>                newPoint.x = l[0]
>                newPoint.y = l[1]
>		#add the point to the line
>                part.add(newPoint)
>		#add the line to the shapeobject
>                newShape.add(part)
>            #print myShape
>	    #add the shapeobject to the shapefileobject
>            myShape.add(newShape)
>        myLayer.status = mapscript.MS_ON
>        myShape=None
>
>Now I get the following Error:
>Traceback (innermost last):
> File "/usr/local/httpd/htdocs/bauinfo/mapwindow.py", line 32, in ? 
>theMap = map.Map(mapfile, 'png', webargs=dct, Conn=dbConn, 
>dynamic=dynamic_layer) 
>File "/usr/local/httpd/htdocs/bauinfo/ms/map.py", line 150, in __init__ 
>self.processMap() 
>File "/usr/local/httpd/htdocs/bauinfo/ms/map.py", line 237, in 
>processMap self._makeDynamicLayer(lay[0], lay[1]) 
>File "/usr/local/httpd/htdocs/bauinfo/ms/map.py", line 400, in 
>_makeDynamicLayer myShape.add(newShape) 
>File "/usr/lib/python2.2/site-packages/mapscript.py", line 336, in add 
>def add(*args): return apply(_mapscript.shapefileObj_add,args) 
>TypeError: Type error. Expected _p_shapefileObj
>
>print myShape returns <C shapefileObj instance at None>
>
>What am I missing?? What goes wrong here??
>
>Any help would be great!
>
>Bye,
>
>Bjoern
>




More information about the mapserver-users mailing list