【问题标题】:check if a lat long is within an extent using open layers 3检查经纬度是否在使用开放层 3 的范围内
【发布时间】:2017-03-30 10:47:47
【问题描述】:

我的地理服务器中有一个使用 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,Lng = -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");
            }
        }

});

问题是我的代码没有打印警报语句。我也尝试过使用

if (ol.extent.containsXY(extent,long,lat))

var XY = ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857');

if (ol.extent.containsXY(extent,XY[0],XY[1])) if (ol.extent.containsXY(extent,XY[1],XY[0]))

但是这些都没有打印警报。这有什么问题吗?

【问题讨论】:

  • 你确定你的 bottomLeft 和 topRight 是对的吗? wfs后你得到的espg是什么?

标签: javascript geolocation openlayers-3 geoserver


【解决方案1】:

在回答你的问题之前,我不知道“ol.extent.containsXY”的方法。 我用了我糟糕的逻辑!如果在多边形中,我通过以下方式检测到一个特征:

  1. 特征容器(多边形)转换为坐标[lon, lat]
  2. 检测容器是否包含功能

范围数组规则[minLon, minLat, maxLon, maxLat]

code sn-p: (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');
            }
        }
    }
};

最后,如果我的回答让您感到困惑,请原谅我的英语不好。

【讨论】:

    【解决方案2】:

    我不得不使用

    var XY = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
    

    而不是

    var XY = ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857');
    

    它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2019-10-08
      • 2012-03-16
      相关资源
      最近更新 更多