3

I have a simple OpenLayers map, where I would like to display the OSM map (the base map, in EPSG:900913) with some points on top of it (a vector layer, in EPSG:3763).

I use proj4js. I do convert a point from EPSG:3763 to EPSG:900913, to set the map center. I also added the MousePosition control, and I can see the coordinates in the displayProjection system. so, the conversion runs fine between the two EPSGs.

I created a new vector layer, with the EPSG:3763 projection. I add a point to that layer pragmatically, in the layer projection EPSG:3763.

The point in the layer goes to Africa, into the ocean, around 0,0.

My questions is: Does OpenLayers can reproject the entire layer? If it does, what am I doing wrong?

From the documentation, it not not clear if OL does the reprojection or not of the layer. In one example, the reprojection is done by MapServer. On the other, the reprojection is done when reading the geojson.

An alternative solution, is to convert the point coordinates to EPSG:900913 before adding them to the vector layer. But I really would like to see how to reproject an entire vector layer, in a different coordinate system.

My code is:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Multiple projections</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script src="http://svn.osgeo.org/metacrs/proj4js/trunk/lib/proj4js-compressed.js"></script>
        <script src="http://spatialreference.org/ref/epsg/3763/proj4js/"></script>
        <link rel="stylesheet" type="text/css" href="http://dev.openlayers.org/releases/OpenLayers-2.13.1/theme/default/style.css" />
        <script src="http://dev.openlayers.org/releases/OpenLayers-2.13.1/lib/OpenLayers.js"></script>
        <script>
            var map = null;
            function init() {
                var options = {
                    controls : [new OpenLayers.Control.MousePosition(), new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoom(), new OpenLayers.Control.LayerSwitcher()],
                    units : "m",
                    maxExtent : new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508.34),
                    maxResolution : 156543.0399,
                    numZoomLevels : 19,
                    projection : "EPSG:900913",
                    displayProjection : new OpenLayers.Projection("EPSG:3763")
                };
                map = new OpenLayers.Map('map', options);
                map.addLayer(new OpenLayers.Layer.OSM("Simple OSM Map"));

                var bounds = new OpenLayers.Bounds(-119191.40749999962, -300404.80399999936, 162129.08110000013, 276083.7674000006);
                var vector_layer = new OpenLayers.Layer.Vector("Point", {
                    units : "m",
                    projection : "EPSG:3763",
                    maxExtent : bounds,
                    maxResolution : 2251.90848203125
                });
                vector_layer.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-22000, 108000), {
                    name : "foo",
                    description : "bar"
                })]);
                map.addLayer(vector_layer);

                var fromProjection = map.displayProjection;
                var toProjection = new OpenLayers.Projection("EPSG:900913");
                var position = new OpenLayers.LonLat(-22000, 108000).transform(fromProjection, toProjection);
                console.log(position);
                map.setCenter(position, 8);
            }
        </script>
    </head>
    <body onload="init()">
        <div id='map' style='width: 400px; height: 500px;'></div>
    </body>
</html>

Happy mapping!

5
  • Related topic: gis.stackexchange.com/questions/88621/…
    – evv_gis
    Apr 14, 2014 at 15:08
  • Yes, the topic is related, but does not answer my question.
    – jgrocha
    Apr 14, 2014 at 15:12
  • You asked if OpenLayers can reproject the entire layer. The post I provided shows how that is done using preFeatureInsert option when constructing the vector layer.
    – evv_gis
    Apr 14, 2014 at 15:21
  • Probably was I not clear. Thanks for your comments @evv_gis. In the topic you mentioned, and in the corresponding fiddle, the vector layer is in the map projection. With preFeatureInsert you are transforming the points before they get into the vector layer. So, in fact, you have a vector layer in the SAME projection as the map. My question is about adding a vector layer in another projection, ie, a layer with features with geometries in another projection different from the map.
    – jgrocha
    Apr 14, 2014 at 15:39
  • Correct, the option is transforming the geometries before loading on the map. A map can only have one projection, and for the data on the map to display in the correct location, it needs to be in the same projection. You can most certainly add another layer in a different projection, but without preprocessing the geometries to match the map projection, it will not appear correctly on the map. The option in the post is a way to preprocess the data so you don't have to individually transform each point.
    – evv_gis
    Apr 14, 2014 at 15:57

1 Answer 1

2

OpenLayers can reproject entire layer, but only at load time (from file or a server). Features added manually (using the addFeatures method) must have their geometries reprojected before adding to a layer.

Your Answer

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

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