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 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 ?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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);
    }
});
share|improve this answer
    
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 Aug 2 '12 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 Aug 2 '12 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 Aug 2 '12 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 Aug 3 '12 at 3:20

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.