[OpenLayers-Users] Extract/read attributes from GML

Pierre GIRAUD bluecarto at gmail.com
Wed Nov 14 17:13:48 EST 2007


Hum, sorry to come out late on this topic but there might be a better
and already approved technique to achieve this.
This uses less code I think but more important you don't reinvent the wheel.
Examples of implementations available in vector-formats.html and
wkt.htmlt (examples folder).

g =  new OpenLayers.Format.GML();
features = g.read(req.responseText);
var bounds;
if(features.constructor != Array) {
    features = [features];
}
for(var i=0; i<features.length; ++i) {
    if (!bounds) {
        bounds = features[i].geometry.getBounds();
    } else {
        bounds.extend(features[i].geometry.getBounds());
    }
}
map.zoomToExtent(bounds);

Regards,
Pierre

On Nov 14, 2007 7:53 PM, springrider <springrider at hotmail.com> wrote:
>
> Effectively, it was a namespace problem.
>
> This is my finalized code to obtain the current extent of my polygons.Could
> be useful to someone else:
>
>                   g =  new OpenLayers.Format.GML();
>                   features = g.read(req.responseText);
>                   for (var i=0;i<features.length;i++) {
>                         var feature = features[i];
>                         var bounds = feature.geometry.getBounds();
>                         if (i==0){
>                                 var minx=bounds.left;
>                                 var miny=bounds.bottom;
>                                 var maxx=bounds.right;
>                                 var maxy=bounds.top;
>                         }
>                    minx = Math.min(minx, bounds.left);
>                    miny = Math.min(miny, bounds.bottom);
>                    maxx = Math.max(maxx, bounds.right);
>                    maxy = Math.max(maxy, bounds.top);
>                         }
>                    alert(minx & " " & miny & " " & maxx & " " & maxy);
>                    map.zoomToExtent(new OpenLayers.Bounds(minx, miny, maxx, maxy));
>
> It works beautifully to extract the extent of my displayed polygons
>
> Thanks
>
> Sylvain
>
>
>
>
> Arnd Wippermann wrote:
> >
> > Hi,
> > propably you have problems with the namespaces. This is, what I get when I
> > load the gml with IE6:
> >
> > The XML page cannot be displayed
> > Cannot view XML input using XSL style sheet. Please correct the error and
> > then click the Refresh button, or try again later.
> >
> >
> > ----------------------------------------------------------------------------
> > ----
> >
> > Reference to undeclared namespace prefix: 'wfs'. Error processing resource
> > 'http://www.nabble.com/file/p13750828/gml.xml'. ...
> >
> > <wfs:FeatureCollection xsi:schemaLocation="http://www.fao.org/fi
> > http://193.43.36.238:8282/geoserver/wfs/DescribeFeatureT...
> >
> >
> > Mit freundlichen Grüssen
> >
> > Arnd Wippermann
> > http://gis.ibbeck.de/ginfo/
> >
> >
> >
> > -----Ursprüngliche Nachricht-----
> > Von: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org] Im
> > Auftrag von springrider
> > Gesendet: Mittwoch, 14. November 2007 17:44
> > An: users at openlayers.org
> > Betreff: Re: [OpenLayers-Users] Extract/read attributes from GML
> >
> >
> > Thanks for your answer
> >
> > I think i was not clear, I used to be a XML/XSL developper so i wanted to
> > access this specific tag. After browsing the different examples and mainly
> > the GMLparser example, i found some answers. I can obtain using OL code
> > the
> > bouding box of my polygon directly.
> >
> > This is a my current code:
> >
> > g =  new OpenLayers.Format.GML();
> > features = g.read(req.responseText);
> > var bounds = features[1].geometry.getBounds();
> >
> > It works fine with the XMLs available for this OL example but with my GML,
> > i
> > always have features.length=0. By doing Alert(req.responseText), i obtain
> > effectively my GML but no features are considered in it. In attachment,
> > you
> > will find my GML: http://www.nabble.com/file/p13750828/gml.xml gml.xml .
> >
> > I really hope you can help me.
> >
> > Thanks
> >
> > Sylvain
> >
> >
> >
> > Christopher Schmidt-4 wrote:
> >>
> >> On Wed, Nov 14, 2007 at 07:17:08AM -0800, springrider wrote:
> >>>
> >>> Good morning
> >>>
> >>> I would be interested to extract/read text within some tags
> >>> (wfs:FeatureCollection\gml:boundedBy\gml:Box\gml:coordinates) from my
> >>> GML. I am now struggling trying to write the correct syntax. I tried
> >>> something like
> >>> that:
> >>>  var myGml= new
> >>> OpenLayers.Layer.GML("myGML","http://193.43.36.238:8282/geoserver/wfs
> >>> ?request=GetFeature&typename=fifao:Fao_areas&cql_filter=F_AREA=27");
> >>> var myExtent=
> >>> myGml.read("wfs:FeatureCollection\gml:boundedBy\gml:Box\gml:coordinat
> >>> es")
> >>>
> >>> But it is not correct.Could you please help me? I would then use this
> >>> text to define the extent of my map.
> >>
> >> This isn't going to do what you want it to do.
> >>
> >> 1. None of the OpenLayers libraries include support for xpath like
> >> navigation of XML documents.
> >> 2. The GML parser returns features; additional metadata from the
> >> document is ignored.
> >> 3. The Layer is just about displaying data; you're more likely to want
> >> to use the Format (but it still won't do what you want.)
> >>
> >> You'll probably want to use the Format.XML class to load the data, and
> >> use standard DOM methods to manipulate the loaded XML, or the
> >> Format.XML specific getElementsByTagNameNS and the like.
> >>
> >> OpenLayers is a map library, rather than a geodata management
> >> framework, so it doesn't provide the tools to do what you want easily,
> >> as far as I'm aware.
> >>
> >> Regards,
> >> --
> >> Christopher Schmidt
> >> MetaCarta
> >> _______________________________________________
> >> Users mailing list
> >> Users at openlayers.org
> >> http://openlayers.org/mailman/listinfo/users
> >>
> >>
> >
> > --
> > View this message in context:
> > http://www.nabble.com/Extract-read-attributes-from-GML-tf4805707.html#a13750
> > 828
> > Sent from the OpenLayers Users mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > Users mailing list
> > Users at openlayers.org
> > http://openlayers.org/mailman/listinfo/users
> >
> > _______________________________________________
> > Users mailing list
> > Users at openlayers.org
> > http://openlayers.org/mailman/listinfo/users
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Extract-read-attributes-from-GML-tf4805707.html#a13753995
>
> Sent from the OpenLayers Users mailing list archive at Nabble.com.
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>



More information about the Users mailing list