Repository: Picterra/picterra-python

Issue:
Fix picterra.nongeo_result_to_pixel to work with list of Polygons
Currently, `picterra.nongeo_result_to_pixel` only works with MultiPolygon (e.g. obtained through the API).

It doesn't work with a list of Polygon such as what you get when exporting from the UI.

We should fix this by making it work with both when loading the data.

Comments:

Patch:
diff --git a/src/picterra/__main__.py b/src/picterra/__main__.py
index 1dc840f..c2004a2 100644
--- a/src/picterra/__main__.py
+++ b/src/picterra/__main__.py
@@ -262,4 +262,5 @@ def main():
         exit("\033[91m%s\033[00m" % e)
 
 
-main()
+if __name__ == '__main__':
+    main()
diff --git a/src/picterra/nongeo.py b/src/picterra/nongeo.py
index bd2dab5..1e368ef 100644
--- a/src/picterra/nongeo.py
+++ b/src/picterra/nongeo.py
@@ -40,6 +40,33 @@ def _nongeo_latlng2xy(lat_deg, lng_deg):
     return x, y
 
 
+def _load_polygons(geojson):
+    """
+    Loads polygons from a geojson file; should work for both MultiPolygon and
+    FeatureCollection of Polygons
+    """
+    polygons = []
+    if geojson['type'] == 'MultiPolygon':
+        for polygon in geojson['coordinates']:
+            polygons.append(polygon)
+    elif geojson['type'] == 'Polygon':
+        polygons = [geojson['coordinates']]
+    elif geojson['type'] == 'FeatureCollection':
+        for feature in geojson['features']:
+            geom = feature['geometry']
+            polygons.extend(_load_polygons(geom))
+    return polygons
+
+
+def _polygon_to_xy(polygon):
+    xy_polygon = []
+    for ring in polygon:
+        xy_polygon.append([
+            _nongeo_latlng2xy(lat, lng) for lng, lat in ring
+        ])
+    return xy_polygon
+
+
 def nongeo_result_to_pixel(result_filename):
     """
     This is a helper function to convert result obtained on non-georeferenced
@@ -65,15 +92,7 @@ def nongeo_result_to_pixel(result_filename):
               ]
     """
     with open(result_filename) as f:
-        multipolygon = json.load(f)
-
-    polygons = []
-    for polygon in multipolygon['coordinates']:
-        xy_polygon = []
-        for ring in polygon:
-            xy_polygon.append([
-                _nongeo_latlng2xy(lat, lng) for lng, lat in ring
-            ])
-        polygons.append(xy_polygon)
-
+        geojson = json.load(f)
+    polygons = _load_polygons(geojson)
+    polygons = [_polygon_to_xy(p) for p in polygons]
     return polygons


Test patch:
diff --git a/tests/test_nongeo.py b/tests/test_nongeo.py
index a8af5e8..9dc5ba0 100644
--- a/tests/test_nongeo.py
+++ b/tests/test_nongeo.py
@@ -1,6 +1,6 @@
 import pytest
 import tempfile
-from picterra.nongeo import _nongeo_latlng2xy
+from picterra.nongeo import _nongeo_latlng2xy, _load_polygons
 from picterra import nongeo_result_to_pixel
 
 
@@ -52,3 +52,99 @@ def test_nongeo_result_to_pixel():
         assert tuple(map(round, polygons[0][0][2])) == (1520, 0)
         assert tuple(map(round, polygons[0][0][3])) == (0, 0)
         assert tuple(map(round, polygons[0][0][4])) == (0, 1086)
+
+
+def test_load_polygons_multipoly():
+    geojson = {
+        "type": "MultiPolygon",
+        "coordinates": [
+            [
+                [
+                    [0.000000096, -0.000975470],
+                    [0.00136530, -0.00097539],
+                    [0.001365320, 0.000000129],
+                    [0.000000034, -0.000000034],
+                    [0.000000096, -0.000975470]
+                ]
+            ]
+        ]
+    }
+    polygons = _load_polygons(geojson)
+    assert len(polygons) == 1
+    assert len(polygons[0][0]) == 5
+    assert polygons[0][0][2][1] == 0.000000129
+
+
+def test_load_polygons_polygon():
+    geojson = {
+        "type": "Polygon",
+        "coordinates": [
+            [
+                [0.000000096, -0.000975470],
+                [0.00136530, -0.00097539],
+                [0.001365320, 0.000000129],
+                [0.000000034, -0.000000034],
+                [0.000000096, -0.000975470]
+            ]
+        ]
+    }
+    polygons = _load_polygons(geojson)
+    assert len(polygons) == 1
+    assert len(polygons[0][0]) == 5
+    assert polygons[0][0][2][1] == 0.000000129
+
+
+def test_load_polygons_fc():
+    geojson = {
+        "type": "FeatureCollection",
+        "features": [
+            {
+                "type": "Feature",
+                "properties": {},
+                "geometry": {
+                    "type": "Polygon",
+                    "coordinates": [
+                        [
+                            [0.000000096, -0.000975470],
+                            [0.00136530, -0.00097539],
+                            [0.001365320, 0.000000129],
+                            [0.000000034, -0.000000034],
+                            [0.000000096, -0.000975470]
+                        ]
+                    ]
+                }
+            },
+            {
+                "type": "Feature",
+                "properties": {},
+                "geometry": {
+                    "type": "MultiPolygon",
+                    "coordinates": [
+                        [
+                            [
+                                [0.000000096, -0.000975470],
+                                [0.00136530, -0.00097539],
+                                [0.001365320, 0.000000129],
+                                [0.000000034, -0.000000034],
+                                [0.000000096, -0.000975470]
+                            ]
+                        ],
+                        [
+                            [
+                                [0.100000096, -0.100975470],
+                                [0.10136530, -0.10097539],
+                                [0.101365320, 0.100000129],
+                                [0.100000034, -0.100000034],
+                                [0.100000096, -0.100975470]
+                            ]
+                        ]
+                    ]
+                }
+            }
+        ]
+    }
+    polygons = _load_polygons(geojson)
+    assert len(polygons) == 3
+    assert len(polygons[0][0]) == 5
+    assert polygons[0][0][2][1] == 0.000000129
+    assert polygons[2][0][2][1] == 0.100000129