The easiest way to verify the equality of OL3 function

I load several points from a geojson file and want to remove duplicates that exist in the data (for some functions the properties are equal, despite the ID). To achieve this, I want to find out if objects are equal to ol.Featureother objects ol.Feature.

Is equality somehow defined for ol.Feature objects, or do I need to define it myself?

+4
source share
3 answers

I think it depends a lot on perspective and usage to tell when the functions are equal, and that is why the user needs to define equality. Some may say that functions are equal if they share the (exact) same geometry (1) . Others may say that functions must have the same properties (2) or even both (3) .

To test the equality property , I would recommend defining attributes that are relevant to your definition of equality. Then you can use code like this to check if 2 ol.Featureobjects are equal :

// Define your important properties    
var mySelectedProperties = ["importantProperty", "anotherImportantProperty", "propertyX"];

// Check for property equality between two ol.Feature objects
function areEqual(featureA, featureB){
    var equal = true;
    for(let property of mySelectedProperties){
        if(featureA.get(property) != featureB.get(property)){
            equal = false;        
            return equal ;
        }
    }
    return equal;
}

For equality of geometry, you can check if the coordinates (x and y) coincide. Here are some more considerations:

  • 2 , :

: lineA: pointA-pointB lineB: pointB-pointA

: polygonA: pointA-pointB-pointC-pointA polygonB: pointB-pointC-pointA-pointB

  • , , , , ... (, () ).
0

. getFeatureById ( ) getId ( ).

, , .

, name , JSON, , , .

var features = [];
var point1 = ol.proj.transform([-50, 4.678], 'EPSG:4326', 'EPSG:3857');
var point2 = ol.proj.transform([20, 4.678], 'EPSG:4326', 'EPSG:3857');

var feature1 = new ol.Feature({
  geometry: new ol.geom.Point(point1),
  name: "First",
  tag: "TAG"
});
var feature2 = new ol.Feature({
  geometry: new ol.geom.Point(point2),
  name: "Second",
  tag: "TAG"
});

features.push(feature1);
features.push(feature2);
features.push(new ol.Feature({
  geometry: new ol.geom.Point(point1),
  name: "First",
  tag: "TAG"
}));

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

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

var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    vectorLayer
  ],
  target: 'map',
  view: new ol.View({
    center: [0, 0],
    zoom: 2
  })
});

document.getElementById("btn").onclick = function(){
  var totalProperties = [];
  vectorSource.getFeatures().forEach(function(feature){
    var propertiesThis = {},
        p = feature.getProperties();

    for (var i in p) {
      if (i === 'name' || i === 'tag') {
        propertiesThis[i] = p[i];
      }
    }
    var jsonProperties = JSON.stringify(propertiesThis);
    
    if (totalProperties.indexOf(jsonProperties) === -1) {
      totalProperties.push(jsonProperties);
    } else {
      vectorSource.removeFeature(feature);
      console.log(propertiesThis['name'] + " feature removed")
    }
  });
};
<link href="https://openlayers.org/en/v3.20.1/css/ol.css" rel="stylesheet"/>
<script src="https://openlayers.org/en/v3.20.1/build/ol.js"></script>
<div id="map" class="map" tabindex="0"></div>

<button id="btn">Remove duplicates</button>
Hide result
+4

ol.Feature .

, , . , , . . JSON ( id geometry) .

(, ):

var uniqueFeatures =  [];
var feature;
var properties;
var json;
var jsons = [];
for (var i = 0, ii = features.length; i < ii; i++) {
  feature = features[0];

  // Stringify the properties of the feature
  properties = feature.getProperties();
  var props4json;
  for (var key in properties) {
    if (key !== 'id' && key !== 'geometry') {
      props4json[key] = properties[key];
    }
  }
  json = JSON.stringify(props4json);

  // Check if the stringified properties exist...
  // if not, we have a new unique feature.
  if (jsons.indexOf(json) === -1) {
    jsons.push(json);
    uniqueFeatures(feature);
  }
}
0

Source: https://habr.com/ru/post/1667040/


All Articles