MapScript lineObj to create a Point?

Sean Gillies sgillies at FRII.COM
Tue Feb 1 09:29:33 EST 2005


On Jan 31, 2005, at 10:13 PM, Tyler Mitchell wrote:

> Am I reading the docs right?
>
> I want to create a point shapefile, but need to first create a lineObj
> and .add() to it.  Then .add() that lineObj to my ShapeObj...then
> .add() that
> to my shapefileObj?
>
> This is the best I can come up with as an example.  Can I shortcut
> this at
> all?
>
> myPoints = mapscript.pointObj(-121, 54)
> myShapes = mapscript.shapeObj(0)
> myLine = mapscript.lineObj()
> myLine.add(myPoints)
> myShapes.add(myLine)
> myShapefile = mapscript.shapefileObj("myshapefile.shp",1)
> myShapefile.add(myShapes)
>
>

Tyler, there is no shortcut to creating a single shape, but if you are
creating *many* point shapes, you can make your code more efficient by
using pointObj::setXY() inside a loop instead of creating a new
pointObj instances for every record in your shapefile:

     shpfile = mapscript.shapefileObj('file.shp', 1)
     temp_point = mapscript.pointObj()

     for (x, y) in coords:
         temp_point.setXY(x, y)
         temp_shape = mapscript.shapeObj(mapscript.MS_SHAPE_POINT)
         temp_line = lineObj()
         temp_line.add(temp_point)
         temp_shape.add(temp_line)
         shpfile.add(temp_shape)

Always use the symbols defined in the module like
mapscript.MS_SHAPE_POINT if you can instead of using the literal
values.  Makes the code easier to understand.

Sean

--
Sean Gillies
sgillies at frii dot com
http://users.frii.com/sgillies



More information about the mapserver-users mailing list