使用开放层 3 检查经纬度是否在一定范围内
check if a lat long is within an extent using open layers 3
我的地理服务器中有一个使用 EPSG:4326 的英国县形状文件(多边形)。我正在使用开放层 3 在我的应用程序中加载此形状文件,如下所示:
source = new ol.source.XYZ({url: '/gmaps?zoom={z}&x={x}&y={y}&Layers=UKCounties', crossOrigin: "anonymous"});
countiesLayer = new ol.layer.Tile({source: source});
map.addLayer(countiesLayer);
这很好用。我需要获取用户的当前位置,这是按
完成的
var coordinate = geolocation.getPosition();
我可以在这里检索到正确的纬度和经度。例如:纬度 = 53.797534899999995,经度 = -1.5449。现在我需要检查这些点在哪些县(多边形)中使用开放层 3 & Javascript。
使用 Geoserver WFS,我能够将每个县的边界框设为
$.each(features, function(index, eachFeature) {
var bbox = eachFeature.properties.bbox;
if (bbox != null) {
var bottomLeft = ([bbox[0], bbox[1]]);
var topRight = ([bbox[2], bbox[3]]);
var extent = new ol.extent.boundingExtent([bottomLeft, topRight]);
if (ol.extent.containsXY(extent1,lat,long)) {
alert("got the feature");
}
}
});
问题是我的代码没有打印警报 statement.I我也试过使用
if (ol.extent.containsXY(extent,long,lat))
和
var XY = ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857');
if (ol.extent.containsXY(范围,XY[0],XY[1]))
如果 (ol.extent.containsXY(范围,XY[1],XY[0]))
但是其中 none 会打印警报。这有什么问题吗?
在回答你的问题之前,我并不知道“ol.extent.containsXY”的方法。
我使用了我糟糕的逻辑!我通过以下方式在多边形中检测到一个特征:
- 将特征和容器(多边形)转换为坐标[lon, lat]
- 如果包含特征
,则检测容器
范围数组规则[minLon, minLat, maxLon, maxLat]
代码片段:(my destinationPro:'EPSG:3857', sourcePro:'EPSG:4326')
QyGIS.prototype.isInner = function(featureExtent, containerExtent) {
var featureLonLat = ol.proj.transformExtent(featureExtent, destinationPro, sourcePro);
var containerLonLat = ol.proj.transformExtent(containerExtent, destinationPro, sourcePro);
// in my condition, the feature is a point, so featureLonLat[0] = featureLonLat[2], featureLonLat[1] = featureLonLat[3]. what's more extent have four value in a array so the loop length is 4
for (var i = 0; i < featureLonLat.length; i++) {
/* actually:
featureLonLat[0] < containerLonLat[0] || featureLonLat[0] > containerLonLat[2]
featureLonLat[1] < containerLonLat[1] || featureLonLat[1] > containerLonLat[3]
featureLonLat[2] < containerLonLat[0] || featureLonLat[2] > containerLonLat[2]
featureLonLat[3] < containerLonLat[1] || featureLonLat[3] > containerLonLat[3]
*/
if (featureLonLat[i] < containerLonLat[i % 2] || featureLonLat[i] > containerLonLat[i % 2 + 2]) {
return false;
}
}
return true;
};
QyGIS.prototype.getInnerFeatures = function(layerName, extent) {
var self = this;
var layer = self.getLayer(layerName);
if (layer) {
var source = layer.getSource();
var features = source.getFeatures();
for (var i = 0; i < features.length; i++) {
var curFeatureExtent = features[i].getGeometry().getExtent();
if (self.isInner(curFeatureExtent, extent)) {
console.log(features[i].get('name') + 'in area');
}
}
}
};
最后,如果我的回答让您感到困惑,请原谅我糟糕的英语。
我不得不使用
var XY = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
而不是
var XY = ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857');
而且有效。
我的地理服务器中有一个使用 EPSG:4326 的英国县形状文件(多边形)。我正在使用开放层 3 在我的应用程序中加载此形状文件,如下所示:
source = new ol.source.XYZ({url: '/gmaps?zoom={z}&x={x}&y={y}&Layers=UKCounties', crossOrigin: "anonymous"});
countiesLayer = new ol.layer.Tile({source: source});
map.addLayer(countiesLayer);
这很好用。我需要获取用户的当前位置,这是按
完成的var coordinate = geolocation.getPosition();
我可以在这里检索到正确的纬度和经度。例如:纬度 = 53.797534899999995,经度 = -1.5449。现在我需要检查这些点在哪些县(多边形)中使用开放层 3 & Javascript。 使用 Geoserver WFS,我能够将每个县的边界框设为
$.each(features, function(index, eachFeature) {
var bbox = eachFeature.properties.bbox;
if (bbox != null) {
var bottomLeft = ([bbox[0], bbox[1]]);
var topRight = ([bbox[2], bbox[3]]);
var extent = new ol.extent.boundingExtent([bottomLeft, topRight]);
if (ol.extent.containsXY(extent1,lat,long)) {
alert("got the feature");
}
}
});
问题是我的代码没有打印警报 statement.I我也试过使用
if (ol.extent.containsXY(extent,long,lat))
和
var XY = ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857');
if (ol.extent.containsXY(范围,XY[0],XY[1])) 如果 (ol.extent.containsXY(范围,XY[1],XY[0]))
但是其中 none 会打印警报。这有什么问题吗?
在回答你的问题之前,我并不知道“ol.extent.containsXY”的方法。 我使用了我糟糕的逻辑!我通过以下方式在多边形中检测到一个特征:
- 将特征和容器(多边形)转换为坐标[lon, lat]
- 如果包含特征 ,则检测容器
范围数组规则[minLon, minLat, maxLon, maxLat]
代码片段:(my destinationPro:'EPSG:3857', sourcePro:'EPSG:4326')
QyGIS.prototype.isInner = function(featureExtent, containerExtent) {
var featureLonLat = ol.proj.transformExtent(featureExtent, destinationPro, sourcePro);
var containerLonLat = ol.proj.transformExtent(containerExtent, destinationPro, sourcePro);
// in my condition, the feature is a point, so featureLonLat[0] = featureLonLat[2], featureLonLat[1] = featureLonLat[3]. what's more extent have four value in a array so the loop length is 4
for (var i = 0; i < featureLonLat.length; i++) {
/* actually:
featureLonLat[0] < containerLonLat[0] || featureLonLat[0] > containerLonLat[2]
featureLonLat[1] < containerLonLat[1] || featureLonLat[1] > containerLonLat[3]
featureLonLat[2] < containerLonLat[0] || featureLonLat[2] > containerLonLat[2]
featureLonLat[3] < containerLonLat[1] || featureLonLat[3] > containerLonLat[3]
*/
if (featureLonLat[i] < containerLonLat[i % 2] || featureLonLat[i] > containerLonLat[i % 2 + 2]) {
return false;
}
}
return true;
};
QyGIS.prototype.getInnerFeatures = function(layerName, extent) {
var self = this;
var layer = self.getLayer(layerName);
if (layer) {
var source = layer.getSource();
var features = source.getFeatures();
for (var i = 0; i < features.length; i++) {
var curFeatureExtent = features[i].getGeometry().getExtent();
if (self.isInner(curFeatureExtent, extent)) {
console.log(features[i].get('name') + 'in area');
}
}
}
};
最后,如果我的回答让您感到困惑,请原谅我糟糕的英语。
我不得不使用
var XY = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
而不是
var XY = ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857');
而且有效。