简体   繁体   English

OpenLayers希望访问多个GeoJSON文件以创建一个或多个矢量层

[英]OpenLayers want to access multiple GeoJSON files to create one or more vector layers

If I had 200+ GeoJSON files defining borders of the world's countries, how would I get this data to appear on the map? 如果我有200多个定义世界各国边界的GeoJSON文件,我如何将这些数据显示在地图上? Reading the multiple files is the part I cannot figure out. 读取多个文件是我无法弄清楚的部分。 I know how to do this for a single GeoJSON file. 我知道如何针对单个GeoJSON文件执行此操作。 Here's my single-file example: 这是我的单文件示例:

var map, layer;

function init() {
  layer = new OpenLayers.Layer.WMS("OpenLayers WMS",
    "http://vmap0.tiles.osgeo.org/wms/vmap0", {
      layers: 'basic'
    }, {
      style: 'min-height:700px'
    });

  map = new OpenLayers.Map('map');
  map.addLayer(layer);
  createCountryBorders(map);

  map.setCenter(new OpenLayers.LonLat(0, 0), 4);
  map.zoomToMaxExtent();
}

function createCountryBorders(map) {
  var geoJsonLayer = new OpenLayers.Layer.Vector("Country Borders", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
      url: "countryborders/sub/countries-hires-line.json",
      format: new OpenLayers.Format.GeoJSON()
    })
  });
  map.addLayer(geoJsonLayer);
}

What kryger said in the answer above: 克雷格在上述答案中说的是:

Note that transferring 200 files over the network and then processing each of them is going to make initialization noticeable (and perhaps even unbearable!) 请注意,通过网络传输200个文件,然后对其进行处理将使初始化变得明显(甚至可能无法忍受!)。

I'de say unbearable and skip the noticable part. 我说难以忍受,并跳过值得注意的部分。 You should take full advantage of the capabilities of GeoJSON, one countryborder should be nothing more than one polygon or a multipolygon. 您应该充分利用GeoJSON的功能,一个国家/地区的边界应该不过是一个多边形或多多边形。 that's one feature. 这是一项功能。 You can create a GeoJSON featurecollection with multiple features, up to thousands if you want and put the entire collection in one file. 您可以创建具有多个要素的GeoJSON要素集合,如果需要,可以创建多达数千个要素,并将整个集合放入一个文件中。

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "id": "AFG",
        "properties": {
            "Afghanistan"
        },
        "geometry": {
            "type": "MultiPolygon".
            "coordinates": [......]
        }
    }, {
        "type": "Feature",
        "id": "ALB",
        "properties": {
            "Albania"
        },
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [......]
        }
    }]
}

Check the GeoJSON spec for FeatureCollection: http://geojson.org/geojson-spec.html#feature-collection-objects 检查用于FeatureCollection的GeoJSON规范: http : //geojson.org/geojson-spec.html#feature-collection-objects

As a matter of fact you don't have to do that yourself. 事实上,您不必自己做。 There are many to find on the net. 在网上可以找到很多东西。 Like the one i used in this example i made for someone here on SO (mind you, it's leaflet but it should get the point across) Here on Plunker: http://plnkr.co/edit/UGQ9xt?p=preview and here's the repo i used on Github: https://github.com/johan/world.geo.json But it you take a look around there are lots of other sources for those world boundary shapes, even in very high detail. 就像我在本示例中使用的那个一样,我是在SO上为某人制作的(请注意,它是传单,但应该可以理解)在Plunker上: http ://plnkr.co/edit/UGQ9xt?p=preview,这是我在Github上使用的仓库: https : //github.com/johan/world.geo.json但是,您可以环顾四周,即使是非常详细的信息,也有很多其他关于这些世界边界形状的资源。

You're not limited to using protocol for loading the data. 您不限于使用protocol来加载数据。 You can read each file and feed it to the GeoJSON parser , getting back an array of Vectors which then can be added to the vector layer. 您可以读取每个文件并将其提供给GeoJSON解析器 ,以获取一个Vector数组,然后可以将其添加到vector层中。 Something like this: 像这样:

var map = // ...
var vectorLayer = // ...
map.addLayer(vectorLayer);

var urls = ['country1.json', 'country2.json', 'country3.json'];
var parser = new OpenLayers.Format.GeoJSON();

urls.each(function(geoJsonUrl) {
  OpenLayers.Request.GET({
    url: geoJsonUrl,
    callback: function(response) {
      var geoJson = response.responseText;
      var vectors = parser.read(geoJson);
      vectorLayer.addFeatures(vectors);
    }
  });
})

Note that transferring 200 files over the network and then processing each of them is going to make initialization noticeable (and perhaps even unbearable!) 请注意,通过网络传输200个文件,然后对其进行处理将使初始化变得明显(甚至可能无法忍受!)。

I was able to get this code to work, based on kryger's suggestion. 根据kryger的建议,我能够使此代码正常工作。 However, what I am really looking for is a way to generate the urls array from a list of files I received from the server. 但是,我真正想要的是一种从我从服务器收到的文件列表中生成urls数组的方法。

Even if I end up with only 10 or so files, I would not want to hardcode their values if I can avoid it. 即使最终只有大约10个文件,如果可以避免的话,我也不想对它们的值进行硬编码。

function createCountryBorders(map) {

    var vectorLayer = new OpenLayers.Layer.Vector("Country Borders");
    var urls = ['country1.json', 'country2.json', 'country3.json'];
    var parser = new OpenLayers.Format.GeoJSON();

    for( var ii in urls ) {
        var file = urls[ii];
        OpenLayers.Request.GET({
            url: 'countryborders/' + file,
            callback: function(response) {
              var geoJson = response.responseText;
              var vectors = parser.read(geoJson);
              vectorLayer.addFeatures(vectors);
            }
      });
    }

    map.addLayer(vectorLayer);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM