Adding a shape to a map dynamically

Ben Tuttle funkbucket at GMAIL.COM
Fri Mar 9 14:27:54 EST 2007


Oops, just had my type and status set incorrectly, switched to the
constants and now it seems to work. This does what I was looking for
and from the responses sounds like a reasonable way to go about it.
The code is below. Thanks to all who responded (and is anyone thinks I
am going about this the wrong way I'd be happy to entertain
alternatives).

bbox = Mapscript::ShapeObj.new(Mapscript::MS_SHAPE_LINE)
p1 = Mapscript::PointObj.new(Request['West'], Request['North'])
p2 = Mapscript::PointObj.new(Request['East'], Request['North'])
p3 = Mapscript::PointObj.new(Request['East'], Request['South'])
p4 = Mapscript::PointObj.new(Request['West'], Request['South'])
line = Mapscript::LineObj.new
line.add(p1)
line.add(p2)
line.add(p3)
line.add(p4)
line.add(p1)
bbox.add(line)

map = Mapscript::MapObj.new "#{map_path}countries.map"
bboxLayer = Mapscript::LayerObj.new(map)
bboxLayer.name = "bbox"
bboxLayer.type = Mapscript::MS_LAYER_LINE
bboxLayer.status = Mapscript::MS_ON
bboxLayer.addFeature(bbox)
cls = Mapscript::ClassObj.new(bboxLayer)
style = Mapscript::StyleObj.new()
color = Mapscript::ColorObj.new()
color.red = 107
color.green = 158
color.blue = 160
style.color = color
cls.insertStyle(style)

image = Mapscript::ImageObj.new(width=800,height=382)
image=map.draw()
bboxLayer.draw(map, image)
image.save("#{image_path}temp.png")
image_url = image.imageurl


On 3/9/07, Ben Tuttle <funkbucket at gmail.com> wrote:
> I was planning on using ruby/mapscript. I am trying to build a small
> interface to be viewed through a web browser. I've considered some of
> the clients, but they tend to be more than I need, so I was thinking
> I'd just build it with mapscript. Your suggestion of adding a point to
> a layer sounds about like what I want to do. The only difference would
> be that we have quite a few mapfiles so ideally I'd like to add the
> layer dynamically as well, rather than add it to every mapfile. I have
> been playing with the code below, but it doesn't show the box. Any
> chance this is a limitation of adding the layer dynamically or am I
> just implementing it incorrectly?
>
> bbox = Mapscript::ShapeObj.new(2)
> p1 = Mapscript::PointObj.new(Request['West'], Request['North'])
> p2 = Mapscript::PointObj.new(Request['East'], Request['North'])
> p3 = Mapscript::PointObj.new(Request['East'], Request['South'])
> p4 = Mapscript::PointObj.new(Request['West'], Request['South'])
> line = Mapscript::LineObj.new
> line.add(p1)
> line.add(p2)
> line.add(p3)
> line.add(p4)
> line.add(p1)
> bbox.add(line)
>
> map = Mapscript::MapObj.new "#{map_path}countries.map"
> bboxLayer = Mapscript::LayerObj.new(map)
> bboxLayer.name = "bbox"
> bboxLayer.type = 2
> bboxLayer.status = 0
> bboxLayer.addFeature(bbox)
> cls = Mapscript::ClassObj.new(bboxLayer)
> style = Mapscript::StyleObj.new()
> color = Mapscript::ColorObj.new()
> color.red = 107
> color.green = 158
> color.blue = 160
> style.color = color
> cls.insertStyle(style)
>
> image = Mapscript::ImageObj.new(width=800,height=382)
> image=map.draw()
> bboxLayer.draw(map, image)
> image.save("#{image_path}temp.png")
> image_url = image.imageurl
>
>
> On 3/9/07, Steve Lime <Steve.Lime at dnr.state.mn.us> wrote:
> > What technology options do you have? Are you using MapScript, the CGI, any particular client?
> >
> > Many of the clients have their own abilities here.
> >
> > With MapScript I would define your presentation layer ahead of time in the mapfile, build a shapeObj and add it to the layer, then draw the layer or map.
> >
> > With the CGI you can define inline features at runtime. Here's an example (w/a point):
> >
> >   http://maps.dnr.state.mn.us/cgi-bin/mapserv410?map=/usr/local/mapserver/apps/waters/csg/reference.map&mode=map&layer=site&map_site_feature_points=342601.236380747+5193344.65190589
> >
> > There is a presentation layer called "site" that I add one point to. Same approach would apply to a polygon, just more points (first=last).
> >
> > Steve
> >
> > >>> On 3/9/2007 at 10:26 AM, in message
> > <259e26270703090826n232af065ndf0436ec711e2489 at mail.gmail.com>, Ben Tuttle
> > <funkbucket at GMAIL.COM> wrote:
> > > Hi all-
> > > I'd like to overlay a polygon on my map based on coordinates input
> > > form a form. I'm wondering what the best way would be to do this.
> > > My first thoughts were to either add a layer dynamically as an inline
> > > feature or perhaps to create a shapeObj (not sure if this will work as
> > > expected) and draw it. Any thoughts on the best way to do something
> > > like this?
> >
> >
>



More information about the mapserver-users mailing list