2017-01-17 48 views
4

我從一個geojson文件加載多個點,並希望刪除數據中存在的重複項(對於某些功能儘管ID爲相同的屬性)。爲了達到這個目標,我想找出是否ol.Feature對象是等於到其他ol.Feature對象。最簡單的方法來檢查OL3功能相等

是否在ol.Feature對象上以某種方式定義了相等性,還是我必須自己定義它?

回答

0

我認爲這取決於很多的觀點和使用情況下,當功能等於這就是爲什麼它留給用戶定義平等地說。有些人可能會說,如果他們共享(確切)相同的幾何圖形,那麼這些功能就是相同的(1)。其他人可能會說功能需要具有相同的屬性(2)或甚至兩個(3)

要檢查屬性相等我會建議定義重要的屬性爲您定義的相等性。然後你可以使用類似下面的代碼檢查,如果2個ol.Feature對象相等:

// 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; 
} 

對於幾何平等你可能要檢查(X & Y)座標是相同的。這裏是一些更多的考慮:

  • 2件的幾何形狀看起來一樣,但座標是不以相同的順序:

例如:LINEA:點A-pointBlineB:pointB-點A

,甚至這樣的:黃精:點A-pointB-pointC-點ApolygonB:pointB-pointC-點A-pointB

  • 對於某些特徵,可能有意義的是說幾何圖形與另一個圖形非常接近,以至於它可能代表相同的特徵...(例如(小)測量誤差或浮點不準確性)。
+0

好的想法。使用LineString功能,幾何考量對我有很大的幫助! – Bel

0

兩個ol.Feature具有完全相同屬性的對象將不相等。

所以是的,你需要手動清除重複。你說這些ID總是獨一無二的,但其餘的有時可能是相同的。在這種情況下,您可以循環使用您的功能。對於每一個,獲取所有屬性的JSON字符串(ID和幾何體除外),並比較一個新的功能集合。

這裏是你如何能做到這一點(未經測試,但是這可能給你一個想法):

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); 
    } 
} 
4

您應該遍歷所有功能並獲得其屬性。 ID將始終不同,因此無法使用方法getFeatureById(來自圖層或來源)或方法getId(來自單個功能)。

我創建了一個生動的例子,當您按下按鈕時,它正在工作並刪除重複的功能。

請注意,我們所得到的性能標籤,我們將它們轉換成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({ 
 
\t features: features 
 
}); 
 

 
var vectorLayer = new ol.layer.Vector({ 
 
\t 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>

相關問題