2

I am trying to insert feature like point, line, etc on "india_ds" wfs layer map using wfs-t but not able to make successful POST request using openlayers3. I have post my script and error. I have linked my "india_ds" layer with PostGIS. Also, do I have to make any changes from my geoserver account and postGIS? I am new to openlayers and geoserver. Please help.

 <script>

      var formatWFS = new ol.format.WFS();

      var formatGML = new ol.format.GML({
          featureNS: 'http://localhost:8080/geoserver/cite',
          featureType: 'india_ds1',
          srsName: 'EPSG:3857'
      });



      var vectorSource = new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        url: function(extent) {
          return 'http://localhost:8080/geoserver/cite/ows?service=WFS&' +
              'version=1.0.0&request=GetFeature&typeName=cite:india_ds1&' +
              'outputFormat=application/json&srsname=EPSG:3857&' +
              'bbox=' + extent.join(',') + ',EPSG:3857';
        },
        strategy: ol.loadingstrategy.bbox
      });



      var vector = new ol.layer.Vector({
        source: vectorSource
      });

      var interaction;

var interactionSelectPointerMove = new ol.interaction.Select({
    condition: ol.events.condition.pointerMove
});

var interactionSelect = new ol.interaction.Select({
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#FF2828'
        })
    })
});

var interactionSnap = new ol.interaction.Snap({
    source: vector.getSource()
});

      var view =  new ol.View({
        center: [0, 0],
        zoom: 2
      });

      var map = new ol.Map({
        target: 'map',
        interactions: [
            interactionSelectPointerMove,
            new ol.interaction.MouseWheelZoom(),
            new ol.interaction.DragPan()
        ],
        layers: [vector],
        view: view
      });

      var dirty = {};
var transactWFS = function (mode, f) {
    var node;
    switch (mode) {
        case 'insert':
            node = formatWFS.writeTransaction([f], null, null, formatGML);
            break;
        case 'update':
            node = formatWFS.writeTransaction(null, [f], null, formatGML);
            break;
        case 'delete':
            node = formatWFS.writeTransaction(null, null, [f], formatGML);
            break;
    }
    var xs = new XMLSerializer();
    var payload = xs.serializeToString(node);
    var geo = f.getGeometry();
    var geometry = geo.getCoordinates();

var postData = '<wfs:Transaction service="WFS" version="1.0.0"\
  xmlns:wfs="http://www.opengis.net/wfs"\
  xmlns:cite="http://www.openplans.org/cite"\
  xmlns:gml="http://www.opengis.net/gml"\
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
  xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd http://www.openplans.org/cite http://localhost:8080/geoserver/wfs/DescribeFeatureType?typename=cite:india_ds1">\
  <wfs:Insert>\
    <cite:india_ds1>\
      <cite:geom>\
        <gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">\
          <gml:lineStringMember>\
            <gml:LineString>\
             <gml:LineString srsName="EPSG:3857">\
              <gml:coordinates decimal="." cs="," ts=" ">\
'+geometry+'\
              </gml:coordinates>\
               </gml:LineString>\
            </gml:LineString>\
          </gml:lineStringMember>\
        </gml:MultiLineString>\
      </cite:geom>\
      <cite:TYPE>alley</cite:TYPE>\
    </cite:india_ds1>\
  </wfs:Insert>\
</wfs:Transaction>';

    $.ajax({
        type: 'POST',
        url:'http://localhost:8080/geoserver/cite/ows',
        dataType: 'xml',
        processData: false,
        contentType: 'text/xml',
        data: postData,
        success: function(response){
          console.log(" point features were posted to server");
        console.log(response);
        }
    }).done();

};

$('button').click(function () {
    $(this).siblings().removeClass('btn-active');
    $(this).addClass('btn-active');
    map.removeInteraction(interaction);
    interactionSelect.getFeatures().clear();
    map.removeInteraction(interactionSelect);

    switch ($(this).attr('id')) {

        case 'btnEdit':
            map.addInteraction(interactionSelect);
            interaction = new ol.interaction.Modify({
                features: interactionSelect.getFeatures()
            });
            map.addInteraction(interaction);
            map.addInteraction(interactionSnap);
            dirty = {};
            interactionSelect.getFeatures().on('add', function (e) {
                e.element.on('change', function (e) {
                    dirty[e.target.getId()] = true;
                });
            });
            interactionSelect.getFeatures().on('remove', function (e) {
                var f = e.element;
                if (dirty[f.getId()]) {
                    delete dirty[f.getId()];
                    var featureProperties = f.getProperties();
                    delete featureProperties.boundedBy;
                    var clone = new ol.Feature(featureProperties);
                    clone.setId(f.getId());
                    transactWFS('update', clone);
                }
            });
            break;

        case 'btnPoint':
            interaction = new ol.interaction.Draw({
                type: 'Point',
                source: vector.getSource(),
                geometryName:'geom'
            });
            map.addInteraction(interaction);
            interaction.on('drawend', function (e) {
              var feature = e.feature;
              feature.set('geom', feature.getGeometry());
                transactWFS('insert', e.feature);
            });
            break;

        case 'btnLine':
            interaction = new ol.interaction.Draw({
                type: 'LineString',
                source: vector.getSource(),
                    geometryName:'geom'
            });
            map.addInteraction(interaction);
            interaction.on('drawend', function (e) {
              var feature = e.feature;
              feature.set('geom', feature.getGeometry());
                transactWFS('insert', e.feature);
            });
            break;

        case 'btnArea':
            interaction = new ol.interaction.Draw({
                type: 'MultiPolygon',
                source: vector.getSource(),
                geometryName:'geom'
            });
            interaction.on('drawend', function (e) {

              var feature = e.feature;
              feature.set('geom', feature.getGeometry());
                transactWFS('insert', e.feature);
                });

            map.addInteraction(interaction);
            break;

        case 'btnDelete':
            interaction = new ol.interaction.Select();
            interaction.getFeatures().on('add', function (e) {
                transactWFS('delete', e.target.item(0));
                interactionSelectPointerMove.getFeatures().clear();
                interaction.getFeatures().clear();
            });
            map.addInteraction(interaction);
            break;

        default:
            break;
    }
});

    </script>

error :- <ServiceException> java.lang.RuntimeException: Parsing failed for coordinates: java.lang.IllegalStateException: Unable to usecom.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory@2cb1c829 to produce CoordinateSequence with dimension 6 Parsing failed for coordinates: java.lang.IllegalStateException: Unable to usecom.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory@2cb1c829 to produce CoordinateSequence with dimension 6 Unable to usecom.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory@2cb1c829 to produce CoordinateSequence with dimension 6 </ServiceException>

1
  • Have you tried to paste the request in GeoServer demo section? there is an option to check if whether the wfst is working or not.
    – neogeomat
    Commented Apr 22, 2019 at 13:13

2 Answers 2

1

From the error message it looks like GeoServer can't understand the coordinates you have sent, it seems to think you are using 6 dimensional space.

I suspect that this line is the issue:

<gml:coordinates decimal="." cs="," ts=" ">\
 '+geometry+'\
</gml:coordinates>

Since your geometry is just an array of coordinates it is unlikely to end up as valid GML. You need to provide a list of coordinates separated by , (that's what cs="," means) and spaces separating the points (ts=" "), so something like:

<gml:coordinates decimal="." cs="," ts=" ">
   x1,y2 x2,y2 x3,y3 ...
</gml:coordinates>
0

Not a definitive answer but I see at least 2 issues

  • your code contains a function with a wrong signature. writeTransaction uses writeTransaction(inserts, updates, deletes, options) whereas your code don't do thing this way as the last argument is an instance of ol.format.GML instead of an object with options. See the API doc

  • you don't need to use postData (normally, you should just drop it) but instead payload. data: postData, should be data: payload,

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.