1

So I've been using the Draw interaction in OpenLayers version 7.4.2 and also 16.15.1 to create mostly Polygons and Multipolygons. I have also used the library ol-ext and it's DrawHole interaction to create inner rings on said Polygons.

The thing is, when making holes on a Multipolygon only one of the Polygons is correctly displayed as having holes, once I retrieve the Geometry from my PostGIS database.

I use GeoJSON to display the geometries in OL, in PostGIS I store them as the Geometry data type.

This is my Geometry viewed with PgAdmin geometry viewer: MultiPolygon with holes in PostGIS

And this is what happens when I display it back in my browser with OpenLayers: MultiPolygon with holes in OL

Only one of the Polygons has holes in them, and if I add more Polygons, they don't display their holes either.

EDIT: This is the GeoJSON I'm trying to display:

{"type":"MultiPolygon","crs":{"type":"name","properties":{"name":"EPSG:3857"}},
    "coordinates":[
        [
            [
                [-4927898.304949742,3417970.934224196],
                [-5336991.054700617,3070757.76006589],
                [-4518805.555198868,3118886.318860111],
                [-4927898.304949742,3417970.934224196]
            ],
            [
                [-5278549.233307635,2946998.608880752],
                [-4436299.454408776,2964187.379878688],
                [-4814452.416363366,2606660.943121621],
                [-5278549.233307635,2946998.608880752]
            ],
            [
                [-4941649.321748091,3307962.799837406],
                [-5123850.294326212,3180765.89445268],
                [-4742259.578172035,3191079.157051441],
                [-4941649.321748091,3307962.799837406]
            ],
            [
                [-5027593.17673777,2864492.508090659],
                [-4601311.65598896,2902307.804286119],
                [-4797263.64536543,2733857.848506346],
                [-5027593.17673777,2864492.508090659]
            ]
        ]
    ]
}

And this is how I create the Vector for the OL Map object:

var source =  new ol.source.Vector({
                    features:  new ol.format.GeoJSON().readFeatures({"type":"MultiPolygon", ... }),                                            
                });
         
var vector = new ol.layer.Vector({
                source: source,
            });

Here is another example. The shape I'm trying to show should have a hole in the triangle below Cyprus:

Cyprus with a triangle

And the GeoJSON:

{"type":"MultiPolygon","crs":{"type":"name","properties":{"name":"EPSG:3857"}},
    "coordinates":[
        [
            [
                [3643685.107557679,4182926.430096696],
                [3664590.034621538,4175823.67931659],
                [3694802.658985702,4187433.059031568],
                [3716271.340274175,4186015.085015787],
                [3724296.209083774,4177672.769468397],
                [3726510.957823341,4163927.967145664],
                [3732062.211157579,4169139.995343562],
                [3748727.476348552,4166308.900514637],
                [3769994.81684252,4176607.571538306],
                [3781925.697065536,4171834.771832592],
                [3785406.016513425,4160905.127019038],
                [3671297.559375653,4105851.047055454],
                [3616803.235194057,4123410.770354937],
                [3590795.757138001,4177918.869344807],
                [3643685.107557679,4182926.430096696]
            ],
            [
                [3534007.85277816,4084808.191431685],
                [3695407.556652446,4050677.071257553],
                [3599377.625315058,3988778.260094297],
                [3534007.85277816,4084808.191431685]
            ],
            [
                [3581444.324884581,4055305.019755741],
                [3639872.174674198,4040264.187136632],
                [3610369.002998253,4011918.002585234],
                [3581444.324884581,4055305.019755741]
            ]
        ]
    ]
}

I have run out of ideas of what might cause this. Could it be a problem with OL?

3
  • A GeoJSON with two MultiPolygons with holes displays correctly in OpenLayers for me. Please include an example of a GeoJSON which does not.
    – Mike
    Jun 9, 2023 at 19:24
  • see stackoverflow.com/questions/43645172/…
    – Mapperz
    Jun 9, 2023 at 20:27
  • Thank you. I have edited to show my geojson
    – Gladr
    Jun 9, 2023 at 21:33

1 Answer 1

2

In the first case the coordinates represent a MultiPolygon containing one polygon with three holes, but two of those holes are outside the polygon. In the second case the MultiPolygon contains one polygon which has two external holes.

To display correctly you would need

  coordinates: [
    [
      [
        [-4927898.304949742, 3417970.934224196],
        [-5336991.054700617, 3070757.76006589],
        [-4518805.555198868, 3118886.318860111],
        [-4927898.304949742, 3417970.934224196]
      ],
      [
        [-4941649.321748091, 3307962.799837406],
        [-5123850.294326212, 3180765.89445268],
        [-4742259.578172035, 3191079.157051441],
        [-4941649.321748091, 3307962.799837406]
      ]
    ],
    [
      [
        [-5027593.17673777, 2864492.508090659],
        [-4601311.65598896, 2902307.804286119],
        [-4797263.64536543, 2733857.848506346],
        [-5027593.17673777, 2864492.508090659]
      ],
      [
        [-5278549.233307635, 2946998.608880752],
        [-4436299.454408776, 2964187.379878688],
        [-4814452.416363366, 2606660.943121621],
        [-5278549.233307635, 2946998.608880752]
      ]
    ]
  ]

and

  coordinates: [
    [
      [
        [3643685.107557679, 4182926.430096696],
        [3664590.034621538, 4175823.67931659],
        [3694802.658985702, 4187433.059031568],
        [3716271.340274175, 4186015.085015787],
        [3724296.209083774, 4177672.769468397],
        [3726510.957823341, 4163927.967145664],
        [3732062.211157579, 4169139.995343562],
        [3748727.476348552, 4166308.900514637],
        [3769994.81684252, 4176607.571538306],
        [3781925.697065536, 4171834.771832592],
        [3785406.016513425, 4160905.127019038],
        [3671297.559375653, 4105851.047055454],
        [3616803.235194057, 4123410.770354937],
        [3590795.757138001, 4177918.869344807],
        [3643685.107557679, 4182926.430096696]
      ]
    ],
    [
      [
        [3534007.85277816, 4084808.191431685],
        [3695407.556652446, 4050677.071257553],
        [3599377.625315058, 3988778.260094297],
        [3534007.85277816, 4084808.191431685]
      ],
      [
        [3581444.324884581, 4055305.019755741],
        [3639872.174674198, 4040264.187136632],
        [3610369.002998253, 4011918.002585234],
        [3581444.324884581, 4055305.019755741]
      ]
    ]
  ]
1
  • That was it! The incorrect GeoJSON is the one PostGIS generates when I select the Geometry from the database. I'll have to check that. Thank you
    – Gladr
    Jun 11, 2023 at 9:56

Your Answer

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

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