私は現在、Openlayers 3 のポリゴン選択ツールに取り組んでおり、ここに投稿されたコードを開発しています。
上記の例は、アプリケーションがロードされたときの検索可能なレイヤー (この場合は WFS) を表示しますが、私の WFS レイヤーには検索する必要がある 80,000 以上のフィーチャが含まれているため、WFS レイヤーのみが表示されるようにこれを適応させようとしています。ユーザーが検索ポリゴンを完了すると、読み込み時間が短縮され、描画されたポリゴンの境界ボックス内のフィーチャのみが表示されます。
次に、JSTS ライブラリを使用して、ユーザーが描画したポリゴンとマップに追加された WFS フィーチャとの間の空間交差を行います。
以下のコードは、描画されたポリゴンの範囲に WFS フィーチャを正しく表示するので問題なく動作しますが、コンソールにフィーチャの属性が返されません。
これは、フィーチャの属性を返す前にレイヤーが完全に読み込まれていないためでしょうか? forEachFeatureInExtent メソッドを実行する前に、レイヤーが読み込まれるまで待機する何かを含める必要がありますか?
var myDrawSource = new ol.source.Vector({wrapX: false});
var myDrawVector = new ol.layer.Vector({
source: myDrawSource,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.5)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
var mySelectionsSource = new ol.source.Vector({wrapX: false});
var mySelectionsVector = new ol.layer.Vector({
source: mySelectionsSource,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 0.5)'
}),
stroke: new ol.style.Stroke({
color: 'rgba(255, 0, 0, 1)',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
var map = new ol.Map({
layers: [raster,myDrawVector,mySelectionsVector],
target: 'map',
view: new ol.View({
projection: bng,
resolutions: resolutions,
center: [501776, 167214],
zoom: 5
})
});
var draw = new ol.interaction.Draw({
source: myDrawSource,
type: "Polygon",
});
map.addInteraction(draw);
draw.on('drawend',function(e){
myDrawSource.clear();
mySelectionsSource.clear();
var waterAreasVecSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: function() {
var featuresExtent = e.feature.getGeometry().getExtent();
return '../../geoserver/wfs?service=WFS&' +
'version=1.1.0&request=GetFeature&typename=waterfeature&' +
'outputFormat=application/json&srsname=EPSG:27700&' +
'bbox=' + featuresExtent.join(',') + ',EPSG:27700';
},
strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
maxZoom: 13
}))
});
var waterAreasVector = new ol.layer.Vector({
source: waterAreasVecSource
});
map.addLayer(waterAreasVector);
var extent = e.feature.getGeometry().getExtent();
var geomA = e.feature.getGeometry();
waterAreasVecSource.forEachFeatureInExtent(extent,function(aa){
console.log("forEachFeatureInExtent",aa.get('name'));
if (polyIntersectsPoly(geomA,aa.getGeometry()) === true){
mySelectionsSource.addFeature(aa);
}
});
});
/**
* check whether the supplied polygons have any spatial interaction
* @{ol.geometry.Polygon} polygeomA
* @{ol.geometry.Polygon} polygeomB
* @returns {Boolean} true||false
*/
function polyIntersectsPoly(polygeomA, polygeomB) {
var geomA = new jsts.io.GeoJSONReader().read(new ol.format.GeoJSON().writeFeatureObject(
new ol.Feature({
geometry: polygeomA
})
)
).geometry;
var geomB = new jsts.io.GeoJSONReader().read(new ol.format.GeoJSON().writeFeatureObject(
new ol.Feature({
geometry: polygeomB
})
)
).geometry;
return geomA.intersects(geomB);
};