根据当前边界框将GeoJSON图层从Geoserver加载到Leaflet Map

时间:2014-08-07 16:46:25

标签: leaflet geojson geoserver bounding-box

目前,我的地图有超过25000点。当我加载所有点时,地图非常慢。因此,我只想在某个缩放级别和边界框(用户视图)加载数据。如何使用我当前的代码实现这一目标?

var map = new L.Map('map', {center: new L.LatLng(54.0000, -125.0000), zoom: 5});
  var googleLayer = new L.Google('ROADMAP');      
  map.addLayer(googleLayer);

function BoundingBox(){
var bounds = map.getBounds().getSouthWest().lng + "," +     map.getBounds().getSouthWest().lat + "," + map.getBounds().getNorthEast().lng + "," + map.getBounds().getNorthEast().lat;
return bounds;
}
var geoJsonUrl ="http://localhost:8080/geoserver/Wells/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=Wells:bc_well_data_wgs&maxFeatures=25&outputFormat=text/javascript&format_options=callback:loadGeoJson"; 

var geojsonLayerWells = new L.GeoJSON();

function loadGeoJson(data) {
console.log(data);
geojsonLayerWells.addData(data);
};

$.ajax({ 
    url: geoJsonUrl, 
    dataType : 'jsonp',
    success: loadGeoJson
    });


map.on('moveend', function(){

    if(map.getZoom() >= 18){

        map.removeLayer(geojsonLayerWells);

        }
    if(map.getZoom() < 18){
        map.addLayer(geojsonLayerWells);
        }
        console.log(map.getZoom());
        console.log(BoundingBox());
    });

5 个答案:

答案 0 :(得分:7)

以下是我如何解决它,改变了一切。

var wellmaxzoom = 11;       
var geojsonLayerWells = new L.GeoJSON();

function loadGeoJson(data) {
    console.log(data);
    geojsonLayerWells.addData(data);
    map.addLayer(geojsonLayerWells);
};

map.on('moveend', function(){
 if(map.getZoom() > wellmaxzoom){
    var geoJsonUrl ='http://localhost:8080/geoserver/cite/ows'; 
    var defaultParameters = {
        service: 'WFS',
        version: '1.0.0',
        request: 'getFeature',
        typeName: 'cite:bc_well_data_wgs',
        maxFeatures: 3000,
        outputFormat: 'application/json'
        };

    var customParams = {
        bbox: map.getBounds().toBBoxString(),
        };
    var parameters = L.Util.extend(defaultParameters, customParams);
    console.log(geoJsonUrl + L.Util.getParamString(parameters));

    $.ajax({
        url: geoJsonUrl + L.Util.getParamString(parameters),
        datatype: 'json',
        jsonCallback: 'getJson',
        success: loadGeoJson
        });
    }else{
    map.removeLayer(geojsonLayerWells);
    };
});

答案 1 :(得分:1)

WFS没有缩放级别或缩放的概念,即WMS,而它支持基于bbox加载数据子集(参见WFS示例)。

那就是说GeoServer不支持Won的JSon,请参阅http://docs.geoserver.org/latest/en/user/services/wms/outputformats.html#wms-output-formats

尽管如此,这可能是一件好事。

西蒙。

答案 2 :(得分:1)

这适用于带有Leaflet 0.7.3的GeoServer 2.6.2。顺便说一句,SF城市的以下bbox号码是由Leaflet的本地功能获得的

var yourMap = L.map('map').setView([37.7749295, -122.4194155], 17);
yourMap.getBounds().toBBoxString();

不限制:(返回很多) http://localhost:8080/geoserver/topp/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=topp:states&maxFeatures=50&outputFormat=application/json

使用地图边界限制(返回当前覆盖) http://localhost:8080/geoserver/topp/ows?service=wfs&version=1.0.0&request=GetFeature&typeName=topp:states&bbox=-122.42553591728209,37.77453948196016,-122.4133050441742,37.775319671408&outputFormat=application/json

答案 3 :(得分:0)

您的代码看起来不错。只需在网址中传递bbox。

var bbox = BoundingBox();

var geoJsonUrl ="http://localhost:8080/geoserver/Wells/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=Wells:bc_well_data_wgs&maxFeatures=25&outputFormat=json&bbox="+bbox+"&format_options=callback:loadGeoJson";

答案 4 :(得分:0)

这就是我想出来的,当我检查加载了多少个对象时,它表示4000,这意味着它也会将所有内容加载到边界框之外....

//loads the google map
var map = new L.Map('map', {center: new L.LatLng(54.0000, -125.0000), zoom: 5});
  var googleLayer = new L.Google('ROADMAP');      // Possible types: SATELLITE,        ROADMAP, HYBRID
  map.addLayer(googleLayer);


//Gets the current bounding box lat and long

function BoundingBox(){
var bounds = map.getBounds().getSouthWest().lng + "," + map.getBounds().getSouthWest().lat + "," + map.getBounds().getNorthEast().lng + "," + map.getBounds().getNorthEast().lat;
return bounds;
}

//loads the well markers
wellmaxzoom = 8;
var geoJsonUrl ="http://localhost:8080/geoserver/cite/ows? service=WFS&version=1.0.0&request=GetFeature&typeName=cite:bc_well_data_wgs&maxFeatures=4000&outputFormat=application/json";

var geojsonLayerWells = new L.GeoJSON();

function loadGeoJson(data) {
console.log(data);
geojsonLayerWells.clearLayers();
geojsonLayerWells.addData(data);
};

$.ajax({ 
    url: geoJsonUrl + "&bbox=" + BoundingBox(),
    dataType : 'json',
    jsonpCallback: 'loadGeoJson',
    success: loadGeoJson,
    });

map.on('moveend', function(){

    if(map.getZoom() > wellmaxzoom){
        map.addLayer(geojsonLayerWells);
        }
        console.log(map.getZoom());
        console.log(BoundingBox());
    });