[OpenLayers-Users] parsing a GML file into different layers
Arnd Wippermann
arnd.wippermann at web.de
Tue Jun 29 16:35:26 EDT 2010
Hi Alex,
that's how I would do it. Use an Ajax request to get the GML-file and then
parse it and add every feature to an new vector layer.
If your GML-file residents on the same server (use relative path to the
gml), then you don't need a proxy script.
If have tested the below code locally with the example
"spherical-mercator.html".
var formats = {
wkt: new OpenLayers.Format.WKT(),
geojson: new OpenLayers.Format.GeoJSON(),
georss: new OpenLayers.Format.GeoRSS(),
gml: new OpenLayers.Format.GML(),
kml: new OpenLayers.Format.KML()
};
function getGML()
{
theUrl = "/OLClient/some.gml";
var myAjax = new OpenLayers.Ajax.Request(
theUrl,
{
method: 'get',
onComplete: showResponse,
onFailure: showResponseFailure
});
}
var arr = [];
function showResponse(response)
{
var theParser = formats["gml"];
theParser.internalProjection = null;
theParser.externalProjection = null;
theParser.extractStyles = false;
theParser.extractAttributes = true;
var features = theParser.read(response.responseText);
if(features)
{
if(features.constructor != Array)
features = [features];
//iterate the features and put them on a vector layer
for(var i=0;i<features.length;i++)
{
//get the feature type
var ftType =
features[i].geometry.CLASS_NAME.replace(/OpenLayers.Geometry./,"");
//count the feature of a type
arr[ftType] = arr[ftType] + 1;
var ftLyr = new OpenLayers.Layer.Vector(ftType + " " +
arr[ftType]);
map.addLayer(ftLyr);
ftLyr.addFeatures(features[i]);
}
//to zoom to the features
var bounds = features[0].geometry.getBounds().clone();
for(var i=1;i<features.length;i++)
bounds.extend(features[i].geometry.getBounds());
map.zoomToExtent(bounds,false);
}
else
alert('Bad input ' + type);
}
function showResponseFailure(response)
{
alert(response);
}
getGML();
Arnd
________________________________
Von: Alex Brandsen [mailto:alex.brandsen at gmail.com]
Gesendet: Dienstag, 29. Juni 2010 12:23
An: Arnd Wippermann
Cc: users at openlayers.org
Betreff: Re: [OpenLayers-Users] parsing a GML file into different layers
Hi Arnd,
thanks for your reply, this seems like it should work. I've been trying it
out, but can't get it to work.. What should go in the brackets after 'GML',
and where does 'response' come from? Sorry if these are really basic
questions, but I don't have much experience in JS or OL...
Thanks again,
Alex.
On Sun, Jun 27, 2010 at 2:44 PM, Arnd Wippermann <arnd.wippermann at web.de>
wrote:
Hi,
something like this should work:
var features = new OpenLayers.Format.GML().read(response);
if(features)
{
if(features.constructor != Array)
features = [features];
for(var i=0;i<features.length;i++)
{
var ftLyr = new OpenLayers.Layer.Vector("Feature " + i);
addLayer(ftLyr);
ftLyr.addFeatures(features[i]);
}
}
Arnd
________________________________
Von: users-bounces at openlayers.org
[mailto:users-bounces at openlayers.org] Im Auftrag von Alex Brandsen
Gesendet: Sonntag, 27. Juni 2010 14:43
An: Eric Lemoine
Cc: users at openlayers.org
Betreff: Re: [OpenLayers-Users] parsing a GML file into different
layers
Hi Eric,
thanks for your reply. I probably need to use OpenLayers.
Format.GML.read() , but how do I then split the array it returns into
different layers?
Thanks,
Alex.
On Sat, Jun 26, 2010 at 7:12 AM, Eric Lemoine
<eric.lemoine at camptocamp.com> wrote:
On Friday, June 25, 2010, Alex Brandsen
<alex.brandsen at gmail.com> wrote:
> Hi all,
>
> I'm building a map of a medieval wallpainting, and the
graffiti inscribed in this wall, which can be found on
www.thomasav.com/DurhamGraffiti/ (still quite buggy though!).
> I was wondering if there is a way for OpenLayers to
dynamically turn features from a single GML file into seperate layers, by
providing an array of feature id's. I've been looking through the
documentation and been googling around, but I can't seem to find anything
useful. Perhaps "OpenLayers. Filter. FeatureId" could do something like
this, but the documentation on this is quite sparse..
>
> Ofcourse, I could just write a php script that would
pre-parse the gml into seperate, temporary gml files, but I think using
OpenLayers itself would be preferable.
>
> If anyone has any ideas, I would be very grateful!
Hi
The protocol or format that you use will give you an array
of
features. You are then free to dispatch the features to
separate
vector layers, for example based on their ids.
I hope it helps,
--
Eric Lemoine
Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex
Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com
More information about the Users
mailing list