Search code examples
postgresqlopenlayersgeoserver

The data entered to the postgres db is altered somehow?


I've created a postgres DB with postgis extension and a table with this command:

create table lgeognad33 (ID serial primary key, geom geometry(POLYGON,3857) not null );

then created workspace and postgis datastore and published that table as a layer in geoserver...this layer will be used by openlayers client as 'WFS' requests...the client code is:

var formatWFS = new WFS();

var formatGML = new GML({
  featureNS: 'https://data.drawns.org/',
  featureType: 'lgeognad33',
  srsName: 'EPSG:3857'
});

var sourceWFS = new VectorSource({
  loader: function (extent) {
    $.ajax('http://localhost:8080/geoserver/SDWS/ows', {
      type: 'GET',
      data: {
      service: 'WFS',
      version: '1.1.0',
      request: 'GetFeature',
      typename: 'lgeognad33',
      srsname: 'EPSG:3857',
     // bbox: extent.join(',') + ',EPSG:3857'
    }
  }).done(function (response) {
  sourceWFS.addFeatures(formatWFS.readFeatures(response));
});
},
//strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ()),
strategy: extent,
projection: 'EPSG:3857'
});

var layerWFS = new VectorLayer({
  source: sourceWFS
});

var map = new Map({
  target: 'map',
  controls: [],
  layers: [
    new TileLayer({
      source: new OSM()
    }), layerWFS
  ],
  view: new View({
 // center: fromLonLat([-1.7, 53.2]),
  center: [4086950.39, 4127574.58],
  zoom: 6
  })
});

var transactWFS = function (p, f) {
  let node;
  switch (p) {
    case 'insert':
      node = formatWFS.writeTransaction([f], null, null, formatGML);
      break;
  }

  var s = new XMLSerializer();
  var str = s.serializeToString(node);
  $.ajax('http://localhost:8080/geoserver/SDWS/lgeognad33/wfs', {
    service: 'WFS',
    type: 'POST',
    dataType: 'xml',
    processData: false,
    contentType: 'text/xml',
    data: str,
  }).done();
}

var interaction;

$('#drawPoly').on('click', function (event) {
  interaction = new Draw({
  type: 'Polygon',
  source: layerWFS.getSource()
  });
  map.addInteraction(interaction);
  interaction.on('drawend', function (e) {
  transactWFS('insert', e.feature);
  });
});

I expect the data inserted in the db to be in epsg:3857 but the result of any inserted polygon is 0 0 0 1 1 1 1 0 0 0 ... i don't know if geoserver is transforming the data or re-projecting it (didn't find any clue why this is happening)...

the request payload sent from the client is:

<Transaction xmlns="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><Insert><lgeognad33 xmlns="https://data.test.org/"><geometry><Polygon xmlns="http://www.opengis.net/gml" srsName="EPSG:3857"><exterior><LinearRing srsName="EPSG:3857"><posList srsDimension="2">4039544.0708149946 4071341.874581627 4144721.421735397 4034652.1010047426 4012638.236858613 3995516.342522732 4039544.0708149946 4071341.874581627</posList></LinearRing></exterior></Polygon></geometry></lgeognad33></Insert></Transaction>

Note: the Native SRS and Declared SRS in geoserver are: epsg:3857

Note: This is a photo of the table, the only different row is a row I've inserted by my hand and it is rendered correctly in the map, all other rows are different polygons submitted from the client database image

Note: the transaction request is:

Request: transaction
    service = WFS
    version = 1.1.0
    baseURL = http://localhost:8080/geoserver/
    group[0] = wfs:insert=net.opengis.wfs.impl.InsertElementTypeImpl@fa0073 (feature: [SimpleFeatureImpl:lgeognad33=[SimpleFeatureImpl.Attribute: geom<geom id=fid--5772524c_1681ace82ae_-7ff7>=POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))]], handle: null, idgen: <unset>, srsName: null)
    insert[0]:
        feature[0] = SimpleFeatureImpl:lgeognad33=[SimpleFeatureImpl.Attribute: geom<geom id=fid--5772524c_1681ace82ae_-7ff7>=POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))]
        idgen = GenerateNew
        inputFormat = text/xml; subtype=gml/3.1.1
    releaseAction = ALL

Solution

  • The problem lays in the name of the geometry field in the database, the default name is "geometry" and this is what geoserver understand but when you use another name like "geom" you should use the setGeometryName function of openlayers feature class to set the name of the geometry for your inserted feaure, I did the following to solve the issue:

    feature.setGeometryName('geom');
    transactWFS('insert', feature);