コード例 #1
0
 def test_object_stewart_two_var(self):
     # Test the OO approach with two variables :
     StePot = SmoothStewart("misc/nuts3_data.geojson", "gdppps2008",
                            span=65000, beta=2, resolution=80000,
                            variable_name2="pop2008",
                            mask="misc/nuts3_data.geojson")
     result = StePot.render(8, "equal_interval", output="Geodataframe")
     self.assertIsInstance(result, GeoDataFrame)
     self.assertEqual(len(result), 8)
コード例 #2
0
    def test_from_polygon_layer_no_crs(self):
        gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson")
        gdf.crs = ''

        # Convert the input layer to a polygon layer (instead of multipolygon):
        gdf.geometry = gdf.geometry.union(gdf.geometry)
        StePot = SmoothStewart(gdf, "gdppps2008",
                               span=65000, beta=2, resolution=100000,
                               mask="misc/nuts3_data.geojson")

        # Use equal interval :
        result = StePot.render(8, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 8)
コード例 #3
0
    def test_from_point_layer_and_maximal_breaks(self):
        gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson").to_crs({"init": "epsg:4326"})

        # Convert the input layer to a point layer :
        gdf.geometry = gdf.geometry.centroid
        StePot = SmoothStewart(gdf, "gdppps2008",
                               span=65000, beta=2, resolution=80000,
                               mask="misc/nuts3_data.geojson")

        # Use equal interval :
        result = StePot.render(9, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 9)

        # Use maximal breaks discretisation method:
        result = StePot.render(9, "maximal_breaks", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
コード例 #4
0
    def test_object_stewart(self):
        # Test the OO approach for building smoothed map with stewart potentials
        StePot = SmoothStewart("misc/nuts3_data.geojson", "pop2008",
                               span=65000, beta=2, resolution=90000,
                               mask="misc/nuts3_data.geojson")

        # Test using percentiles :
        result = StePot.render(nb_class=10,
                               disc_func="percentiles",
                               output="geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 10)

        # Test using somes already choosed break values :
        my_breaks = [0, 197000, 1295000, 2093000, 3091000,
                     5888000, 10186000, 12000000]
        result = StePot.render(
            nb_class=48,  # bogus values as `nb_class` and
            disc_func="foobar",  # ... disc_func should be overrided
            user_defined_breaks=my_breaks,  # ... by the `user_defined_breaks` params
            output="geodataframe")         # ... and this is what we are testing here

        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 8)
        # Assert these break values were actually used :
        for wanted_break, obtained_break in zip(my_breaks[1:-1], result["max"][:-1]):
            self.assertAlmostEqual(wanted_break, obtained_break)

        # Test again using another discretization method : "head tail breaks"
        # (should define automatically the number of class)
        result = StePot.render(nb_class=None,
                               disc_func="head_tail",
                               output="geodataframe")
        self.assertIsInstance(result, GeoDataFrame)

        # Test that the object has a nice representation :
        a = str(StePot)
        b = repr(StePot)
        self.assertEqual(a, b)
        self.assertIn("SmoothStewart - variable :", a)
        self.assertIn("{} features".format(len(StePot.gdf)), a)
コード例 #5
0
    def test_distance_not_geo(self):
        # First whith one variable :
        StePot = SmoothStewart("misc/nuts3_data.geojson",
                               "gdppps2008",
                               resolution=100000,
                               span=65000, beta=3,
                               mask="misc/nuts3_data.geojson",
                               distGeo=False)
        result = StePot.render(8, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 8)

        # Then with two variables and a custom projection to use :
        StePot = SmoothStewart("misc/nuts3_data.geojson",
                               "gdppps2008",
                               span=65000, beta=2,
                               resolution=80000,
                               variable_name2="pop2008",
                               mask="misc/nuts3_data.geojson",
                               distGeo=False,
                               projDistance={"init": "epsg:3035"})
        result = StePot.render(8, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 8)
        self.assertEqual(result.crs, {'init': 'epsg:3035'})
コード例 #6
0
    def test_errors(self):
        # Test with a wrong interaction function name :
        with self.assertRaises(ValueError):
            StePot = SmoothStewart("misc/nuts3_data.geojson", "gdppps2008",
                                   span=65000, beta=2,
                                   typefct="abcdefg")

        StePot = SmoothStewart("misc/nuts3_data.geojson", "gdppps2008",
                               span=65000, beta=2, resolution=90000)

        # Test with a wrong discretization function name :
        with self.assertRaises(ValueError):
            StePot.render(9, "foo", output="Geodataframe")
コード例 #7
0
    def test_wrong_dtype_missing_values(self):
        gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson")
        gdf.loc[12:18, "gdppps2008"] = np.NaN
        gdf.loc[25:35, "pop2008"] = np.NaN
        gdf.loc[0:len(gdf)-1, "pop2008"] = gdf["pop2008"].astype(str)
        StePot = SmoothStewart(gdf, "gdppps2008",
                               span=65000, beta=2, resolution=100000,
                               mask="misc/nuts3_data.geojson")
        result = StePot.render(9, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 9)

        StePot = SmoothStewart(gdf, "gdppps2008", variable_name2="pop2008",
                               span=65000, beta=2, resolution=100000,
                               mask="misc/nuts3_data.geojson")
        result = StePot.render(9, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 9)
コード例 #8
0
    def test_input_with_missing_values(self):
        gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson")
        gdf.loc[12:18, "gdppps2008"] = np.NaN
        StePot = SmoothStewart(gdf, "gdppps2008",
                               span=65000, beta=2, resolution=100000,
                               mask=gdf)
        result = StePot.render(9, "equal_interval", output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 9)

        gdf2 = GeoDataFrame.from_file('misc/nuts3_data.geojson').to_crs({"init": "epsg:3035"})
        gdf2.loc[:, 'gdppps2008'] = gdf2['gdppps2008'].astype(object)
        gdf2.loc[15:20, 'gdppps2008'] = ""
        gdf2.loc[75:78, 'gdppps2008'] = ""
        StePot = SmoothStewart(gdf2, 'gdppps2008', span=65000, beta=2,
                               resolution=80000, mask=gdf2)
        result = StePot.render(9, 'equal_interval', output="GeoDataFrame")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 9)
コード例 #9
0
    def test_from_gdf_with_new_mask(self):
        gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson")

        # Let's use pareto function for this one :
        StePot = SmoothStewart(gdf, "gdppps2008", typefct="pareto",
                               span=65000, beta=2.33, resolution=80000,
                               mask=None)
        result = StePot.render(6, output="Geodataframe")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(len(result), 6)

        # Finally, use a mask (from a file) :
        result = StePot.render(5, output="Geodataframe",
                               new_mask="misc/nuts3_data.geojson")
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(StePot.use_mask, True)
        self.assertEqual(len(result), 5)

        # Or from a GeoDataFrame :
        result = StePot.render(6, output="Geodataframe",
                               new_mask=gdf)
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(StePot.use_mask, True)
        self.assertEqual(len(result), 6)

#        # Nope, no mask :
#        result = StePot.render(5, output="Geodataframe",
#                               new_mask=None)
#        self.assertIsInstance(result, GeoDataFrame)
#        self.assertEqual(StePot.use_mask, False)
#        self.assertEqual(len(result), 5)

        # Test that it skips the mask parameter if the layer provided as a mask
        # is not a Polygon/MultiPolygon layer :
        gdf_mask = gdf[1:50].copy()
        gdf_mask.geometry = gdf_mask.geometry.centroid
        result = StePot.render(5, output="Geodataframe",
                               new_mask=gdf_mask)
        self.assertIsInstance(result, GeoDataFrame)
        self.assertEqual(StePot.use_mask, False)
        self.assertEqual(len(result), 5)
コード例 #10
0
def quick_stewart_mod(input_geojson_points, variable_name, span,
                      beta=2, typefct='exponential',
                      nb_class=None, disc_kind=None, resolution=None,
                      mask=None, variable_name2=None,
                      user_defined_breaks=None):
    """
    Compute a smoothed map (modified function from smoomapy).

    Parameters
    ----------
    input_geojson_points: str
        Path to file to use as input (Points/Polygons), must contains
        a relevant numerical field.
    variable_name: str
        The name of the variable to use (numerical field only).
    span: int
        The span!
    beta: float
        The beta!
    typefct: str, default "exponential"
        The type of function in {"exponential", "pareto"}
    nb_class: int, default None
        The number of class, if unset will most likely be 8.
    resolution: int, default None
        The resolution to use (in unit of the input file), if not set a
        resolution will be used in order to make a grid containing around
        12000 pts.
    mask: str, default None
        Path to the file (Polygons only) to use as clipping mask.
    user_defined_breaks: list or tuple, default None
        A list of ordered break to use to construct the contours
        (override `nb_class` value if any)

    Returns
    -------
    smoothed_geojson: bytes
        The result dumped as GeoJSON (utf-8 encoded).
    breaks: dict
        The break values used.
    """
    StePot = SmoothStewart(
        input_geojson_points, variable_name,
        span, beta, typefct, resolution=resolution,
        variable_name2=variable_name2, mask=mask, distGeo=False,
        projDistance='+proj=natearth')
    result = StePot.render(nb_class,
                           disc_kind,
                           user_defined_breaks,
                           output="GeoDataFrame")
    _min, _max = result[["min", "max"]].values.T.tolist()
    result.to_crs({'init': 'epsg:4326'}, inplace=True)
    if not mask:
        # In some weird cases, when not using a clipping mask, some resulting
        # geometries seems to be malformed, but hopefully saving it
        # to shapefile then reopening it seems to fix that:
        res = save_reload(result)
    else:
        res = json.loads(result[::-1].to_json())
    repairCoordsPole(res)
    return (
        json.dumps(res).encode(),
        {"min": _min[::-1], "max": _max[::-1]})