3

-A layer that contains multiple features - I want to draw new features with new OpenLayers.Control.DrawFeature and to add them to my layer.

   var control = new OpenLayers.Control.DrawFeature(layer,OpenLayers.Handler.Polygon);

control.events.register('featureadded', control, function(f) {  
            f.feature.state = OpenLayers.State.INSERT;
            f.feature.attributes.Geo_ID = 794; 
            saveStrategy.save(); 

       });    

When the feature is added, a xml file is sent to the geoserver with the following content:

<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Insert>
<feature:Graeber xmlns:feature="myserver.de/Ahaus_neu">
<feature:the_geom>
<gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:31466">
<gml:exterior>
<gml:LinearRing>
<gml:posList>363183.0625 5770740.875 363192.0625 5770755.375 363177.5625    5770764.375 363168.5625 5770749.875 363183.0625             5770740.875</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</feature:the_geom>
<feature:Geo_ID>794</feature:Geo_ID>
</feature:Graeber>
</wfs:Insert>
</wfs:Transaction>

The coordinates are not saved in the geoserver, only the feature:Geo_ID Element.

If I choose an existing Element to insert in the geoserver Database instead:

  control.events.register('featureadded', control, function(f) { 

            layer.features[0].state = OpenLayers.State.INSERT;

            f.feature.state = OpenLayers.State.INSERT;
            f.feature.attributes.Geo_ID = 795; 

            saveStrategy.save(); 

       }); 

I get the following XML in the Request:

<wfs:Insert>
<feature:Graeber xmlns:feature="myserver.de/Ahaus_neu" fid="Graeber.1">
<feature:the_geom>
<gml:MultiSurface xmlns:gml="http://www.opengis.net/gml"     srsName="EPSG:31466">
<gml:surfaceMember>
<gml:Polygon>
 <gml:exterior>
<gml:LinearRing>
<gml:posList>363273.29099999997 5770863.855 363273.177000001     5770864.300999998 363274.1561700017 5770864.5512810005 363274.2701699999 5770864.105281 363273.29099999997 5770863.855</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
</gml:MultiSurface>
</feature:the_geom>
<feature:Geo_ID>795</feature:Geo_ID>
</feature:Graeber>
</wfs:Insert>      

And the coordinates DO get inserted in the geoserver DB.

I see one major difference between these two XMLs:

the insertion XML of an existing Element of the layer contains two more tags:

<gml:MultiSurface xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:31466"> and <gml:surfaceMember>

How do I make the application know that the features added by drawing a polygon are to be treated the same way and put in the same format as the features that already exist in the Database?

2 Answers 2

2

What I finally found out that helped:

            f.feature.geometry = new OpenLayers.Geometry.MultiPolygon(f.feature.geometry);
1

You perhaps fail because you are trying to insert a polygon into WFS layer which is of type multipolygon. I would try the "multi" option from http://dev.openlayers.org/docs/files/OpenLayers/Control/DrawFeature-js.html

multi

{Boolean} Cast features to multi-part geometries before passing to the layer. Default is false.

2
  • at least: var control = new OpenLayers.Control.DrawFeature(layer,OpenLayers.Handler.Polygon); control.multi = true; control.events.register('featureadded', control, function(f) { ...} does not work
    – Lokomotywa
    Commented Oct 20, 2014 at 10:24
  • Perhaps code that is used in automatic tests of OpenLayers help you github.com/adube/openlayers/blob/master/tests/Control/… // set on handler control = new OpenLayers.Control.DrawFeature( layer, OpenLayers.Handler.Polygon, {multi: true} ); t.ok(control.handler.multi, "handler.multi set from control options");
    – user30184
    Commented Oct 20, 2014 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.