DrawFeature.featureAdded() - called in wrong place?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

DrawFeature.featureAdded() - called in wrong place?

Jeff Dege
Maybe I don't understand what you intend DrawFeature.featureAdded to be
used for, and it's possible that this whole area of functionality will
be replaced, prior to 2.4, and it's current implementation is moot, but:

I'm trying to create two controls on a tool bar, both of which draw
polygons, but where what happens after the polygon is drawn would be
different.  I had noticed that in EditingToolBar.js, each control is
assigned a featureAdded() function that sets the state of the feature to
OpenLayers.State.INSERT.  I'd thought that I could use this function to
set a different state, or to add my own status attribute to the feature,
and then check on the value of that state in
Layer.Vector.onFeatureInsert().

But in Control\DrawFeature.js, the drawFeature() function calls
this.featureAdded() after it calls this.layer.addFeatures(), so there's
no way in onFeatureInsert() to distinguish which control created the
feature.

As far as I can tell, the feature.state variable isn't actually used for
anything.  Maybe I'm missing something, but would it break anything to
make these calls in reverse order?

Or is all of this going to be scrapped and replaced?

_______________________________________________
Dev mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: DrawFeature.featureAdded() - called in wrong place?

Christopher Schmidt-2
On Thu, Apr 05, 2007 at 04:49:06PM -0500, Jeff Dege wrote:

> Maybe I don't understand what you intend DrawFeature.featureAdded to be
> used for, and it's possible that this whole area of functionality will
> be replaced, prior to 2.4, and it's current implementation is moot, but:
>
> I'm trying to create two controls on a tool bar, both of which draw
> polygons, but where what happens after the polygon is drawn would be
> different.  I had noticed that in EditingToolBar.js, each control is
> assigned a featureAdded() function that sets the state of the feature to
> OpenLayers.State.INSERT.  I'd thought that I could use this function to
> set a different state, or to add my own status attribute to the feature,
> and then check on the value of that state in
> Layer.Vector.onFeatureInsert().


> But in Control\DrawFeature.js, the drawFeature() function calls
> this.featureAdded() after it calls this.layer.addFeatures(), so there's
> no way in onFeatureInsert() to distinguish which control created the
> feature.
>
> As far as I can tell, the feature.state variable isn't actually used for
> anything.  Maybe I'm missing something, but would it break anything to
> make these calls in reverse order?

the State of the feature is used for posting to WFS servers -- depending
on the state, a feature is added, updated or deleted.

I'd probably feel more comfortable offering advice to you if I had a
better idea why you need to know which control added a feature before
it's added -- styling, I presume?

> Or is all of this going to be scrapped and replaced?

No. With a few small exceptions (deprecation of geometry.feature
attribute), OL 2.4 API is what it will be.

Regards,
--
Christopher Schmidt
MetaCarta
_______________________________________________
Dev mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: DrawFeature.featureAdded() - called in wrong place?

Jeff Dege
> From: Christopher Schmidt [mailto:[hidden email]]
> Sent: Thursday, April 05, 2007 5:01 PM
>
> On Thu, Apr 05, 2007 at 04:49:06PM -0500, Jeff Dege wrote:
> >
> > I'm trying to create two controls on a tool bar, both of which draw
> > polygons, but where what happens after the polygon is drawn would be
> > different.  

> the State of the feature is used for posting to WFS servers
> -- depending
> on the state, a feature is added, updated or deleted.
>
> I'd probably feel more comfortable offering advice to you if I had a
> better idea why you need to know which control added a feature before
> it's added -- styling, I presume?

No.  It's a bit more fundamental than that.

We have a map displaying a landbase, and a geographical area that our
users are editing.  The area consists of a set of non-overlapping
polygons that are displayed on the map.  The user is given the choice
between navigate, add, or subtract mode.  Navigate mode works as you'd
expect.  In add or subtract mode, the user draws polygons on the map.
When he's completed drawing a polygon, in onFeatureInsert(), we either
add or subtract the new polygon from the set we'd been displaying.  Not
as in adding the new polygon to the list of polygons, but as in doing a
union or a difference with the existing polygons, resulting in a new set
of polygons.  

Adding one polygon to a set of three polygons may result in one polygon
or four, subtracting one polygon from another may result in one polygon
or three.  depending upon the geometry.  We're not preserving the
identity of the polygons, but instead are preserving the area covered by
them in the simplest way possible.  The user can tweak the borders to
his hearts content.  When he has everything the way he likes, he clicks
on the save button, and it all gets stored away in the appropriate
place.

It's all working fine, thanks to third-party polygon clipping library,
using some plain html controls outside the map.  I'd rather use controls
that are on the map, inside the toolbar.  But that requires that I know
which control created the feature, when onFeatureInsert() is called.

_______________________________________________
Dev mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: DrawFeature.featureAdded() - called in wrong place?

Tim Schaub-2
In reply to this post by Jeff Dege
Jeff Dege wrote:

> I'm trying to create two controls on a tool bar, both of which draw
> polygons, but where what happens after the polygon is drawn would be
> different.  I had noticed that in EditingToolBar.js, each control is
> assigned a featureAdded() function that sets the state of the feature to
> OpenLayers.State.INSERT.  I'd thought that I could use this function to
> set a different state, or to add my own status attribute to the feature,
> and then check on the value of that state in
> Layer.Vector.onFeatureInsert().
>
> But in Control\DrawFeature.js, the drawFeature() function calls
> this.featureAdded() after it calls this.layer.addFeatures(), so there's
> no way in onFeatureInsert() to distinguish which control created the
> feature.

You have 3 or 4 dozen options here.

First, the layer calls onFeatureInsert - so you're right, you have no
way of knowing what did the feature adding by the time that gets called.

The DrawFeature control calls featureAdded (past tense) after the
feature has been added to the layer - so here also, you're too late if
you want to do something with the feature before it gets added to the
layer.  It wouldn't make any sense to call featureAdded before the
feature gets added to the layer.

If you look at the DrawFeature control, you'll see that it sets its own
drawFeature method as the "done" callback for the handler that actually
does the drawing.  So, you're drawing polygons.  The DrawFeature control
is using the PolygonHandler in this case.  The DrawFeature control is
constructing the PolygonHandler with callbacks that look like {done:
this.drawFeature} (where this is the DrawFeature control).

So, while the DrawFeature control is not really meant to be used to do
anything custom with features *before* they are added to a layer (more
on this below) - you could customize this control to do what you want.

If you construct your DrawFeature control with something like:

     var options = {drawFeature: myDrawFeature};
     var control = new OpenLayers.Control.DrawFeature(
                           layer, OpenLayers.Handler.Polygon, options);

and myDrawFeature is something like:

     function myDrawFeature(geometry) {
         var feature = new OpenLayers.Feature.Vector(geometry);
         // do something with feature before it is added to the layer
         // at this point you know which control did the drawing
         layer.addFeatures([feature]);
         control.featureAdded(feature);
     }

All that said, I wonder if you really need to be using the DrawFeature
control at all.  If you want users to digitize polygons, and then you
want to do a lot of custom stuff *before* adding the features to a
layer, then it sounds to me like you should make a custom control that
uses the polygon handler.

I recognize that this is too much for one email already.  If you want to
create your own control that uses the polygon handler (i.e. if the above
solution doesn't look good to you), write back and I can show a quick
example of an alternative.

Hope that helps,
Tim
_______________________________________________
Dev mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: DrawFeature.featureAdded() - called in wrong place?

Jeff Dege
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Tim Schaub
> Sent: Friday, April 06, 2007 11:04 AM
>
> You have 3 or 4 dozen options here.

Of course I do.

> So, while the DrawFeature control is not really meant to be used to do

> anything custom with features *before* they are added to a layer (more

> on this below) - you could customize this control to do what you want.
>
> If you construct your DrawFeature control with something like:
>
>      var options = {drawFeature: myDrawFeature};
>      var control = new OpenLayers.Control.DrawFeature(
>                            layer, OpenLayers.Handler.Polygon,
> options);
>
> and myDrawFeature is something like:
>
>      function myDrawFeature(geometry) {
>          var feature = new OpenLayers.Feature.Vector(geometry);
>          // do something with feature before it is added to the layer
>          // at this point you know which control did the drawing
>          layer.addFeatures([feature]);
>          control.featureAdded(feature);
>      }
>
> All that said, I wonder if you really need to be using the DrawFeature

> control at all.  If you want users to digitize polygons, and then you
> want to do a lot of custom stuff *before* adding the features to a
> layer, then it sounds to me like you should make a custom control that

> uses the polygon handler.

Deriving my own custom control might be cleaner, but what you've
described above is working, so I expect I'll stick with it until I have
reason to revisit this section of code.

Thanks.



_______________________________________________
Dev mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/dev