Adding features to new layerObj (Re: [Mapserver-users] mapscript - strange behavior)

Christoph Spoerri spoerri at duke.edu
Wed Jan 14 12:42:05 PST 2004


Points are actually added one by one. So previous points are stored in the 
.map file when using addFeature().

One thing I observer today (while 'studying' the problem) was that the point 
is actually added, but not with the style I defined. It uses a 1-pixel sized 
dot.

I also tried to add two points at the time (two points in the same feature) or 
two features at the same time, I still don't see the correct style (the pin 
marker).

Now the thing that finally worked (what a hack though!):

	...
	lyrAcetate.addFeature(shpObj)
	self._map.save('/tmp/testing.map')
	tmpMap = mapObj('/tmp/testing.map')
	lyrAcetate = tmpMap.getLayerByName('acetate')
	mapImg=tmpMap.prepareImage()
	lyrAcetate.draw(tmpMap,mapImg)
	...

So, I finally get the functionality I want. Yet I loose some performance when 
adding the first point due to the save/load, but I can live with it.

I was thinking also of submitting a bug report. Does the mapserver project 
have some sort of bug tracker?

Thanks for all your help and patience,
Christoph

On Wednesday 14 January 2004 01:33 pm, Eric Bridger wrote:
> I use addFeature() a lot, for both points and map labels (like your
> acetate or Sean's copyright) (but Perl mapscript 3.6 only).  Something
> here rings a bell, but I can't remember the details.  For points I use
> addFeature() just so the point layer is queryable and in that case I
> always use $point->draw() in addition to $layer->addFeature($point).But
> never $layer-draw(). This is the bell I seem to remember.
>
> Perhaps a work-around in your case would be to use ptObj.draw() instead
> of the lyrAcetate.draw().  It's not clear to me from your code where the
> additional points are coming from anyway. The lyrAcetate only has 1
> point in it as far as I can see.
>
> On Wed, 2004-01-14 at 12:32, Sean Gillies wrote:
> > Cristoph,
> >
> > I haven't been using inline features of a layer like you are doing,
> > so I don't know yet if there are any problems with it.  I have changed
> > the subject line of the message to see if we can attract other users
> > with experience.
> >
> > The method I have used is to define a layer like your acetate layer,
> > and then used the draw method of point or shape objects.  Here's an
> > example of how I do this using an acetate layer named 'copyright':
> >
> >      # Draw copyright label
> >      layerobj = mapobj.getLayerByName('copyright')
> >      layerobj.status = MS_ON
> >      layerobj.getClass(0).label.position = MS_UR
> >      p = pointObj()
> >      p.x, p.y = (1, mapobj.height-1)
> >      p.draw(mapobj, layerobj, imgobj, 0, copyright_text)
> >
> > I still think that you could predefine acetate layers in your map file
> > for each of point, line, and polygons.  In my opinion, your application
> > may
> > benefit from having as many as possible of the map element styles
> > defined
> > in the map file.  Try to seperate the logic (your Python code) from
> > the presentation (the classes and styles defined in the map file) as
> > much as possible.
> >
> > OK, enough advice from me :)  Let's see if anyone with experience using
> > a layerObj's addFeature will join in and help you.
> >
> > cheers,
> > Sean
> >
> > On Jan 14, 2004, at 9:50 AM, Christoph Spoerri wrote:
> > > One thing I wanted to add: the reason I need to create the 'acetate'
> > > layer on
> > > the fly is that I'm mapping different layer types (point, line,
> > > polygon) and
> > > therefore the classes change. I'm using a pin as point marker, and red
> > > lines
> > > for lines/polygons.
> > >
> > > BTW, when mapping lines, the first feature (line) does not display
> > > with the
> > > correct class either (at least it shows though). So does anybody know
> > > what
> > > the problem actually is with not showing the first feature correctly
> > > or not
> > > at all?
> > >
> > > Thanks, Christoph
> > >
> > > On Tuesday 13 January 2004 02:28 pm, Sean Gillies wrote:
> > >> On Jan 13, 2004, at 10:38 AM, Christoph Spoerri wrote:
> > >>> Hi there,
> > >>>
> > >>> I'm writing an application that let's user add point features to the
> > >>> map by
> > >>> clicking on any location of the map. For this purpose I'm using the
> > >>> attached
> > >>> code (Note: each click on the map results in a request to the server
> > >>> for a
> > >>> new image).
> > >>>
> > >>> The problem is that the first point (to be mapped) is not displayed
> > >>> on
> > >>> the
> > >>> map. Yet when the user clicks on the map for the second time (to add
> > >>> a
> > >>> new
> > >>> point) both points show up. When checking the .map files everything
> > >>> looks
> > >>> fine: acetate layer is there, class definition looks fine and point
> > >>> coordinates are there.
> > >>>
> > >>> In case a predefine the acetate layer (w/ class def) in the base .map
> > >>> file,
> > >>> the first point shows up just fine. Note: this solution is not an
> > >>> option
> > >>> thought :(
> > >>>
> > >>> any ideas? Thanks in advance,
> > >>> Christoph
> > >>>
> > >>>
> > >>> *********** code ********************
> > >>>
> > >>> 	x = atof(param['x'])
> > >>> 	y = atof(param['y'])
> > >>> 	# create point object
> > >>>  	ptCoord = getMapPoint([x,y],self._map)
> > >>>  	ptObj = pointObj()
> > >>>  	ptObj.x=ptCoord[0]
> > >>>  	ptObj.y=ptCoord[1]
> > >>> 	lnObj = lineObj()
> > >>> 	lnObj.add(ptObj)
> > >>>
> > >>> 	# create new point layer with class and add shape
> > >>> 	mapImg=self._map.prepareImage()
> > >>> 		lyrAcetate = self._map.getLayerByName('acetate')
> > >>> 	if (not isinstance(lyrAcetate,layerObj)):
> > >>> 		color = colorObj()
> > >>> 		color.red = 255
> > >>> 		color.green = 255
> > >>> 		color.blue = 255
> > >>> 		lyrAcetate = layerObj(self._map)
> > >>> 		lyrAcetate.name = "acetate"
> > >>> 		lyrAcetate.type = MS_LAYER_POINT
> > >>> 		classTemp = classObj(lyrAcetate)
> > >>> 		styl = styleObj()
> > >>> 		styl.symbolname = "pin"
> > >>> 		styl.size = 1
> > >>> 		styl.maxsize = 1
> > >>> 		styl.minsize = 1
> > >>> 		styl.color = color
> > >>> 		styl.backgroundcolor = color
> > >>> 		styl.outlinecolor = color
> > >>> 		classTemp.numstyles=1
> > >>> 		classTemp.styles = styl
> > >>>
> > >>> 	# create shape object and add point to shape object
> > >>> 	shpObj = shapeObj(MS_SHAPE_POINT)
> > >>> 	shpObj.add(lnObj)
> > >>> 	shpObj.setBounds();
> > >>> 	shpObj.index = 1
> > >>>
> > >>> 	lyrAcetate.status = MS_ON
> > >>> 	lyrAcetate.addFeature(shpObj)
> > >>> 	lyrAcetate.draw(self._map,mapImg)
> > >>>
> > >>> 	imagename =   \
> > >>> self._map.name+mapSession.id()+"_"+str(int(time()))+"."+mapImg.format
> > >>> .e
> > >>> xtension
> > >>> 	mapImg.save(mapImg.imagepath+imagename)
> > >>
> > >> Christoph,
> > >>
> > >> Do I remember correctly that you are using mod_python?  Is this a
> > >> reason
> > >> why you say that you cannot predefine your acetate layer?  I hope you
> > >> are
> > >> not trying to persist a mapObj in the same way that mod_python users
> > >> use
> > >> persistent database connections.  MapServer is not fully thread-safe
> > >> (yet)
> > >> and so you will certainly encounter some trouble with a persistent
> > >> mapObj.
> > >>
> > >> I don't see _anything_ in your code that would prevent the acetate
> > >> layer
> > >> from being predefined in your map file.  If it is only a matter of
> > >> colors and styles, I strongly recommend that you predefine the acetate
> > >> layer and then modify its classes and styles during the script
> > >> execution.
> > >>
> > >> cheers,
> > >> Sean
> > >>
> > >> --
> > >> Sean Gillies
> > >> sgillies at frii dot com
> > >> http://users.frii.com/sgillies
> >
> > _______________________________________________
> > Mapserver-users mailing list
> > Mapserver-users at lists.gis.umn.edu
> > http://lists.gis.umn.edu/mailman/listinfo/mapserver-users




More information about the MapServer-users mailing list