1

I am new to OpenLayers. In one experiment I am doing I want to load, incrementally, features in a vector layer. In my setting, these features should be loaded from distinct OSM files (generated dynamically) in a HTTP server.

I know how to create a vector layer from a single OSM file, I have done it with a code similar to this:

...
map = new OpenLayers.Map('map_element', {
    maxResolution: 156543.0399,
    numZoomLevels: 16,
    units: 'm', 
    projection: new OpenLayers.Projection('EPSG:900913'),
    displayProjection: new OpenLayers.Projection('EPSG:4326')
});

vectorLayer = new OpenLayers.Layer.Vector("POIs", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
    url: "my_dynamically_generated_osm_file.osm",
    format: new OpenLayers.Format.OSM()
    }),
    projection: new OpenLayers.Projection("EPSG:4326")
});

map.addLayers([<other_layers_here>, vectorLayer]);
...

However, I am wondering how I can create first the vector layer without features, and afterwards adding new features defined in different OSM files according to the requests of the user. I know vector layer objects have a method addFeatures(), so I just need to know how could I obtain these features from the distinct OSM files provided by my http server ?

1 Answer 1

1

For example:

//If projection of source data is different from map projection we have to describe this
var osm = new OpenLayers.Format.OSM({
    'internalProjection': new OpenLayers.Projection("EPSG:900913"),
    'externalProjection': new OpenLayers.Projection("EPSG:4326")
});

vector = new OpenLayers.Layer.Vector()
map.addLayer(vector);

OpenLayers.Request.GET({
    url: "http://gis-lab.info/share/DR/sandbox/myosmfile.osm",
    success: function(e) {
        features = osm.read(e.responseText);
        vector.addFeatures(features);
    }
});
4
  • Thanks for your answer @drnextgis!, one question about the projections: In my example, when I instantiated the vector layer, I added in its properties:"projection: new OpenLayers.Projection("EPSG:4326")" Is this correct considering it is loading OSM data?, or should I had specified the same projection I declared for the map (EPSG:900913)? or rather should I had declared the osm format object as you did in your example (specifying internal and external projections)?. Thanks for any clarification, the projection details are breaking my head now. By default the OSM data is in EPSG:4326, isn't it?
    – Sergio
    Commented Aug 2, 2012 at 9:04
  • 1
    Yes, you are correct. Note that even if you set the projection object on a layer, if you are adding features to the layer manually (via layer.addFeatures), they must be transformed before adding to the layer. OpenLayers will only transform the projection of geometries that are created internally to the library, to prevent duplicating projection work. docs.openlayers.org/library/spherical_mercator.html
    – drnextgis
    Commented Aug 2, 2012 at 9:13
  • I see, so the extra step of declaring the format object with internal and external projections was only necessary because we are adding layers manually. If I understood correctly, in my original code that step was not necessary since when setting the projection property of the layer (with "projection: new OpenLayers.Projection("EPSG:4326")"), I am explicitly saying that the expected features are in the "EPSG:4326" projection, then behind the curtains those features will be transformed to the projection of the map ("EPSG:900913"), correct?
    – Sergio
    Commented Aug 2, 2012 at 11:37
  • Yes, only for EPSG:4326 and EPSG:900913 and their equivalents. Otherwise you should use proj4js library trac.osgeo.org/proj4js
    – drnextgis
    Commented Aug 3, 2012 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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