Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invert a GeoJson-Polygon

Tags:

geojson

Ho can I invert a GeoJson of a country, so that it defines the country as a hole in the polygon of the worldmap? I need this to create a mask of the GeoJson VectorLayer in OpenLayers 5.3.

like image 288
Meiko Rachimow Avatar asked Jan 11 '19 23:01

Meiko Rachimow


2 Answers

In the coordinates array add this array as the the first coordinates and it'll do the invert

    [
      [
        0,
        90
      ],
      [
        180,
        90
      ],
      [
        180,
        -90
      ],
      [
        0,
        -90
      ],
      [
        -180,
        -90
      ],
      [
        -180,
        0
      ],
      [
        -180,
        90
      ],
      [
        0,
        90
      ]
    ],

And you can find examples to exactly what you want to do here https://github.com/minaalfy/city-map

like image 126
Mina paulis Avatar answered Oct 16 '22 15:10

Mina paulis


Here a code snippet for leaflet + GeoJson, to add a world rectangle to the coordinates. That will cut out/invert/reverse the polygon in geoJson.

geoJson.geometry.coordinates[0].unshift([[180, -90], [180, 90], [-180, 90], [-180, -90]]);
L.geoJSON(geoJson, { style: { color: '#ccc', weight: 2, fillOpacity: 0.5 } }).addTo(map);
like image 6
Falk Brückner Avatar answered Oct 16 '22 15:10

Falk Brückner