Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openlayers - How to draw a polygon on both sides of the Date Line

I need to draw a few big polygons on the map. The polygon must be duplicated on all sides of the Date Line. They should be drawn the same as on the google maps My example.

1) Please give your oppinion, How to draw a polygon in case when the polygon crosses the Date Line? I sold this problem the same as in example , but then I had trouble with drawing the polygon when it crosses the Date Line. That is why I added 360 degree to the longitude - is it correct?

for (var i = 1; i < polygonPoints.length; i++) {
      var dY = polygonPoints[i][1] - polygonPoints[i-1][1];
      if(Math.abs(dY) > 180)
           polygonPoints[i][1] += 360;
}

2) Please advise, what is correct way of drawing a polygon on both sides of the Date Line?

Could you please check my variant:

    var map;    // openlayers map

    // [[latitude, longitude], ...]
    var polygonPoints = [[10, -100],[30, -70],[38, -30],[40, 0],[38, 50 ],[20, 100],[50, 170],[55, 180],
                        [50, -170], [40, -160],[10, 170],[0, -170],[-10, -160],[-25, -170], [-30, 100 ],
                        [-45, 10 ],[-50, -20],[-55, 30],[-35, -160],[-40, -160],[-60, 30],  [-50, -30 ],
                        [-40, 10 ],[-25, 100], [-20, -175],[-10, -165],[0, -180 ],[10, 165],[50, -180],
                        [45, 170],[15, 100],[33, 50 ],[35, 0 ],[35, -30],[25, -70],[5, -100],[10, -100] ];
    function InitOL(){
        var wgs84Projection = new OpenLayers.Projection("EPSG:4326");
        var epsg900913 = new OpenLayers.Projection('EPSG:900913');

        var options = {
            div: "openLayersMap",
            projection: wgs84Projection,
            displayProjection: epsg900913,
            allOverlays: true,
            zoom: 1
        };

        map = new OpenLayers.Map(options);
        var mapserv = new OpenLayers.Layer.MapServer( "OpenLayers Basic",
                    "http://vmap0.tiles.osgeo.org/wms/vmap0",
                    {layers: 'basic'},
                    {wrapDateLine: true} );

        var polygonsLayer = new OpenLayers.Layer.Vector("PolygonsLayer", null, { wrapDateLine: true });

        map.addLayers([mapserv, polygonsLayer]);
        map.addControl(new OpenLayers.Control.LayerSwitcher());
        map.addControl(new OpenLayers.Control.MousePosition());
        map.zoomToMaxExtent();
        map.setCenter(new OpenLayers.LonLat(-100, 0));

        for (var i = 1; i < polygonPoints.length; i++) {
            var dY = polygonPoints[i][1] - polygonPoints[i-1][1];
            if(Math.abs(dY) > 180)
                polygonPoints[i][1] += 360;
        }

        var olPoints = [];
        for (var j = 0; j < polygonPoints.length; j++) {
            olPoints.push(new OpenLayers.Geometry.Point(polygonPoints[j][1], polygonPoints[j][0]));
        }
        var linearRing = new OpenLayers.Geometry.LinearRing(olPoints);
        var bounds = new OpenLayers.Bounds([-180, -90, 180, 90]);
        linearRing.setBounds(bounds);

        var polygonStyle = {
                strokeColor: "#770077",
                strokeOpacity: 1,
                strokeWidth: 2,
                fillColor: "#770077",
                fillOpacity: 0.5
            };

        var olPolygon = new OpenLayers.Geometry.Polygon(linearRing);
        var polygonFeature = new OpenLayers.Feature.Vector(olPolygon);
        polygonFeature.style = polygonStyle;
        polygonsLayer.addFeatures(polygonFeature);
    }
like image 972
user2017648 Avatar asked Nov 25 '22 06:11

user2017648


1 Answers

This looks like a limitation of the way OpenLayers currently draws polygons. You probably need to file a bug report and hope it will be patched or patch it yourself... You could generate the second polygon yourself when you detect this case.

like image 132
Christophe Roussy Avatar answered Dec 04 '22 07:12

Christophe Roussy