I'm having trouble with the vector layer for my little project,

here's the snippet of the javascript block

var map;
        function init(){
            map = new OpenLayers.Map('map', {
              projection: new OpenLayers.Projection("EPSG:3857")
            var osm_layer = new OpenLayers.Layer.OSM("OpenStreetMap");

            var path_layer = new OpenLayers.Layer.Vector("Path Layer", {
                  projection: "EPSG:4326",
                  strategies: [new OpenLayers.Strategy.Fixed()],
                  protocol: new OpenLayers.Protocol.HTTP({
                  url: "{% url 'path_json' route.id %}",
                  format: new OpenLayers.Format.GeoJSON()
            map.addControl(new OpenLayers.Control.LayerSwitcher());
            new OpenLayers.LonLat(121.032, 14.594).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), 12);

and here's the view that responds with the GeoJSON object:

def get_path_json(request, route_id):
    route = get_object_or_404(Route, pk=route_id)
    geoj = GeoJSON.GeoJSON()
    path = route.path.all()

    path_format = Django.Django(geodjango="path", properties=['mode'])
    path_json = geoj.encode(path_format.decode(path))

    return HttpResponse(path_json, content_type="application/json")

the object is parsed in geoJSON format via vectorformats.

so.. what am i doing wrong? or am i missing something? The map displays but the vector layer doesn't have the features described in the GeoJSON?

  • I had problems with different EPSGs. When I use a EPSG 4326 WMS map as a base layer, I can draw EPSG4326 vector features on it. If I use a EPSG900913 OSM layer, I cannot (they simply won't display). I have not seen a good article/HOWTO on "how to do reprojection with OpenLayers" and would be glad for a hint. – til_b Jul 9 '13 at 7:27
  • i certainly agree with you. still trying to sort this out – iamjoanel Jul 9 '13 at 14:40

Just in case, I opted to use a different approach, since using the above method wasn't working out.

var map = new OpenLayers.Map('map');
    geojson_parser = new OpenLayers.Format.GeoJSON();
base_layer = new OpenLayers.Layer.OSM("OpenStreetMap");

path_layer = new OpenLayers.Layer.Vector("Path Layer");
train_path_layer = new OpenLayers.Layer.Vector("Train Path Layer");



map.addControl(new OpenLayers.Control.LayerSwitcher());
new OpenLayers.LonLat(121.032, 14.594).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), 12);

and my view just passes the geoJSON along with the other objects to the template.


What I can see now is that your view name in your js template path_json does not match that you defined in views.py get_path_json. And the route object in your js file, from which you get the id property as a parameter pass to the view function, seems not defined (or maybe you defined but not paste it here).

  • as for the route object i just didn't paste the whole template cause it might be an inconvenience, where as the path_json, which was used on the url tag is the url's name? should i use the view name instead ? even if i have it named as 'path_json' on the urls.py? thanks – iamjoanel Jul 9 '13 at 14:37
  • You also need to match the url pattern to the view function, and that's how you route the url to the view function. – mfdev Jul 9 '13 at 16:39
  • At the urls.py i did and used name="path_json". guess ill give that a try – iamjoanel Jul 9 '13 at 17:03
  • okay so i got it to work and checked the firebug console and the json object is indeed being requested. but still not being displayed on the map. any advice? – iamjoanel Jul 10 '13 at 2:04
  • What do you mean the json object is requested? Did you get the json object successfully in the brouwer console? Just cannot display them with openlayers? – mfdev Jul 10 '13 at 6:30

I just did a little search about the projection. The OSM layer is a spherical mercator layer, although it's projection (EPSG:900913) is identical to EPSG:3857, you need to transform it explicitely, you can find more details here.

And actually you don't need to specify the projection for map object, it's project is usually determined by the projection of current selected base layer.

Not sure about this, but maybe good to have a try.

  • i might have forgotten that in my models.py i defined the srid of the geometry as 900913. might it be a factor to the problem? – iamjoanel Jul 10 '13 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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