geojson icon indicating copy to clipboard operation
geojson copied to clipboard

MultiPolygon not parsing although appearing to be valid

Open sptdigital opened this issue 6 years ago • 2 comments

Hello

I am having an issue parsing the following GeoJSON ...

https://gist.github.com/sptdigital/f0b7102bae5e9d72c3845b406c323951

It returns the error message LinearRing requires at least four positions, even though my LinearRings are compliant with the latest version of the GeoJson standard as far as I can see.

I have no issues using this GeoJson anywhere outside of this package.

Could you possibly point me in the right direction please?

Thanks, Ally

sptdigital avatar Feb 28 '19 12:02 sptdigital

I can't reproduce this error. Test code returns successfully, no errors.

$ composer require "jmikola/geojson=~1.0"
<?php
require __DIR__ . '/vendor/autoload.php';

$json = file_get_contents('https://gist.githubusercontent.com/sptdigital/f0b7102bae5e9d72c3845b406c323951/raw/09d2d9fef0603320bdce65d93975badf7e1da63c/example.json');
$json = json_decode($json);
$geojson = \GeoJson\GeoJson::jsonUnserialize($json);

var_dump($geojson);

martinburch avatar Feb 28 '19 14:02 martinburch

I can confirm that the JSON file above appears to be valid. In addition to running the script @martinburch shared, I inspected the data with a JS interpreter:

// After assigning the GeoJSON object to x

x.geometry.coordinates.forEach(function(iv, ik) {
   print("MultiPolygon["+ik+"] has "+iv.length+" Polygon(s)");
   iv.forEach(function(jv, jk) {
     print("  Polygon["+jk+"] has "+jv.length+" coordinates");
   });
 });

This produced the following output:

MultiPolygon[0] has 1 Polygon(s)
  Polygon[0] has 203 coordinates
MultiPolygon[1] has 1 Polygon(s)
  Polygon[0] has 278 coordinates
MultiPolygon[2] has 1 Polygon(s)
  Polygon[0] has 61 coordinates
MultiPolygon[3] has 1 Polygon(s)
  Polygon[0] has 116 coordinates
MultiPolygon[4] has 1 Polygon(s)
  Polygon[0] has 438 coordinates
MultiPolygon[5] has 1 Polygon(s)
  Polygon[0] has 157 coordinates
MultiPolygon[6] has 1 Polygon(s)
  Polygon[0] has 9104 coordinates
MultiPolygon[7] has 1 Polygon(s)
  Polygon[0] has 486 coordinates
MultiPolygon[8] has 1 Polygon(s)
  Polygon[0] has 816 coordinates
MultiPolygon[9] has 2 Polygon(s)
  Polygon[0] has 31847 coordinates
  Polygon[1] has 4 coordinates
MultiPolygon[10] has 1 Polygon(s)
  Polygon[0] has 59 coordinates
MultiPolygon[11] has 1 Polygon(s)
  Polygon[0] has 77 coordinates
MultiPolygon[12] has 1 Polygon(s)
  Polygon[0] has 1591 coordinates

I'll note that the tenth polygon (i.e. MultiPolygon[9]) is the only geometry with an interior ring (hole), which consists of 4 coordinates. Since the first and last coordinates must be equivalent, that would be a triangle representing the smallest closed loop. All other polygons have far more than 4 coordinates in their exterior rings.

If you have an exception, it should be possible to analyze the stack trace and determine exactly what data was passed to the LinearRing constructor via Polygon::__construct().

jmikola avatar Feb 28 '19 16:02 jmikola