[OpenLayers-Users] Layer.GML.features.length

Jeff Dege jdege at korterra.com
Thu Jun 7 11:10:45 EDT 2007


I'm creating a GML layer from a GML file.

I want the map to zoom to the smallest box bounding all the features in
the GML file.

What I've tried is:

	var map = new OpenLayers.Map('map', {controls: []});
		
	var gmlLayer = new OpenLayers.Layer.GML("GML",
"FEATURELIST.GML")
	gmlLayer.isBaseLayer = true;
	map.addLayer(gmlLayer);
		
	var minx = 999999;
	var miny = 999999;
	var maxx = -999999;
	var maxy = -999999;
		
	for (var i = 0; i < gmlLayer.features.length; i++)
	{
		minx = Math.min(minx, gmlLayer.features[i].bounds.minx);
		miny = Math.min(miny, gmlLayer.features[i].bounds.miny);
		maxx = Math.max(maxx, gmlLayer.features[i].bounds.maxx);
		maxy = Math.max(maxy, gmlLayer.features[i].bounds.maxy);
	}
		
	map.zoomToExtent(new OpenLayers.Bounds(minx, miny, maxx, maxy));

The problem is that gmlLayer.features.length == 0.

There are features in the GML file, and they draw on the map just fine
if I use a hard-coded extent.  But they don't seem to be loaded into the
gmlLayer object until the map is actually drawn.

Looking through the code, I found Layers.GML.loadGML(), and I thought
that might do it.  But it doesn't.  That just pulls the file down from
the URL, it doesn't parse the contents or initialize the features array.
That parsing doesn't happen until Layer.GML.requestSuccess() is called
by openLayers.loadURL(), after the GML file has been downloaded in the
background.

There is a hook in the code, onFeatureInsert(), that's called after each
feature has been inserted.  But it's called after each feature has been
inserted.  I could examine the bounds of each feature, and update the
maxextent, then zoom to the new extent, but I'd have to do it after each
feature, calling zoomToExtent() once for every feature, because I have
no way of knowing when I've got the last one.

Seems to me the best hook, for dealing with these sorts of issues, might
be a doneInsertingFeatures() callback.  Added after the end of the loop
in Layer\Vector.js.

Are the problems with this that I'm not envisioning?


    /**
     * @param {Array(OpenLayers.Feature.Vector} features
     */
    addFeatures: function(features) {
        if (!(features instanceof Array)) {
            features = [features];
        }

        for (var i = 0; i < features.length; i++) {
            var feature = features[i];
            
            if (this.geometryType &&
                !(feature.geometry instanceof this.geometryType)) {
                    var throwStr = "addFeatures : component should be an
" + 
 
this.geometryType.prototype.CLASS_NAME;
                    throw throwStr;
                }

            this.features.push(feature);
            
            //give feature reference to its layer
            feature.layer = this;

            if (!feature.style) {
                feature.style = OpenLayers.Util.extend({}, this.style);
            }

            this.preFeatureInsert(feature);

            if (this.drawn) {
                this.drawFeature(feature);
            }
            
            this.onFeatureInsert(feature);
        }

        this.doneInsertingFeatures();
    },





More information about the Users mailing list