Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to send requests to our feature server that asks only data within the viewable map extent. So I've used BBOX strategy and HTTP Protocol as following code.

        mVectorLayer = new OpenLayers.Layer.Vector("Overlay", {
            **strategies: [new OpenLayers.Strategy.BBOX()],**
            protocol: new OpenLayers.Protocol.HTTP({
                url: 'http://localhost:56786/jlist.geojson',
                format: new OpenLayers.Format.GeoJSON({
                    'read': myReadFunction,
                    'internalProjection': map.baseLayer.projection,
                    'externalProjection': new OpenLayers.Projection("EPSG:4326")
                })
            }),
            projection: new OpenLayers.Projection("EPSG:900913")
        });

I've added a feature outside the viewable map to the geojson file shown at below.

{ "type": "FeatureCollection",
  "features": [
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [29.0, 41.060]},
        "properties": {"name": "IST J1", "img": "img/marker.png"}
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [29.0, 41.100]},
        "properties": {"name": "IST J2", "img": "img/marker.png"}
      },
      **{ "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [59.0, 41.100]},
        "properties": {"name": "IST J3", "img": "img/marker.png"}
      }**
  ]
}

To validate, I've added an alert to the function myReadFunction which shows the json string. But the alert shows all features in geojson file. I suppose our feature server sends all geojson content instead of viewable features? How can I validate or observe whether the BBOX strategy works successfully?

    function myReadFunction(json, type, filter) {
        **alert("json: " + json);**

        type = (type) ? type : "FeatureCollection";
        var results = null;
        var obj = null;
        if (typeof json == "string") {
            obj = OpenLayers.Format.JSON.prototype.read.apply(this, [json, filter]);
        } else {
            obj = json;
        }
        if (!obj) {
            OpenLayers.Console.error("Bad JSON: " + json);
        } else if (typeof (obj.type) != "string") {
            OpenLayers.Console.error("Bad GeoJSON - no type: " + json);
        } else if (this.isValidType(obj, type)) {
            switch (type) {
                case "Geometry":
                    try {
                        results = this.parseGeometry(obj);
                    } catch (err) {
                        OpenLayers.Console.error(err);
                    }
                    break;
                case "Feature":
                    try {
                        results = this.parseFeature(obj);
                        results.type = "Feature";
                    } catch (err) {
                        OpenLayers.Console.error(err);
                    }
                    break;
                case "FeatureCollection":
                    // for type FeatureCollection, we allow input to be any type
                    results = [];
                    switch (obj.type) {
                        case "Feature":
                            try {
                                results.push(this.parseFeature(obj));
                            } catch (err) {
                                results = null;
                                OpenLayers.Console.error(err);
                            }
                            break;
                        case "FeatureCollection":
                            for (var i = 0, len = obj.features.length; i < len; ++i) {
                                try {results.push(this.parseFeature(obj.features[i]));
                                } catch (err) {
                                    results = null;
                                    OpenLayers.Console.error(err);
                                }
                            }
                            break;
                        default:
                            try {
                                var geom = this.parseGeometry(obj);
                                results.push(new OpenLayers.Feature.Vector(geom));
                            } catch (err) {
                                results = null;
                                OpenLayers.Console.error(err);
                            }
                    }
                    break;
            }
        }
        return results;

}

Thanks a lot for your helps and explanations, Yasemin

share|improve this question
add comment

1 Answer 1

you are requesting data from a user without specifying the BBOX of features you want the server to return, so I understand the server is always returning a data no matter if their inside/outside the BBOX.

Note new OpenLayers.Strategy.BBOX() strategy is used to update the data (request again) when the map's viewport change. If you want to request the data form a file only once and not refresh when the map is panned then use the new OpenLayers.Strategy.Fixed() strategy.

To check your features you can create a map listener on the "moveend" event, then get the features from your layer and check which are withint the map viewport BBOX.

Cheers.

share|improve this answer
    
Note, the book OpenLayers Cookbook (packtpub.com/openlayers-create-gis-web-applications-cookbook/…) has some nice recipes to deal with features and events. –  EricSonaron Sep 1 '12 at 19:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.