[Mapserver-users] Creating dynamic shapefiles with Python Mapscript

Bjoern Platzen bplatzen at sosnetz.de
Tue Nov 18 03:45:38 EST 2003


Hi Folks,

I have some point-data in a MySQL database. Just showing them was not a 
problem. But now I also want to make them queryable. As far as I found 
out in the ML-archive, in that case, I'll have to create a new 
(temporary) shapefile and a corresponding dbf, right?

So I tried to write a funtion for my class to produce these shapefiles:

def _makeDynamicLayer(self, layer, queryfile):
        shpName = 'dds_data/'+layer
	#define a new shapefile object
        myShape = mapscript.shapefileObj(shpName, 
mapscript.MS_SHAPE_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
-- 
small office solutions
info at sosnetz.de  -  http://www.sosnetz.de




More information about the mapserver-users mailing list