From c5fbd0c6f832200790611bb8247f63e2b19ba32b Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 13 Apr 2021 17:02:32 -0300 Subject: [PATCH 01/53] add create_agg function docstring --- mapshader/core.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 75d8074..4d5c056 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -34,6 +34,28 @@ def create_agg(source: MapSource, x: float = None, y: float = None, z: float = None, height: int = 256, width: int = 256): + """ + Instantiate an abstract canvas representing the space and compute + a reduction by pixel according to the geometry type applying the + inputted aggregation function. + + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + xmin : float + X-axis minimum range. + ymin : float + Y-axis minimum range. + xmax :float + X-axis maximum range. + ymax : float + Y-axis maximum range. + x, y, z : float + The coordinates to be used to get the bounds inclusive space along the axis. + width, height : int, optional + Width and height of the output aggregate in pixels. + """ if x is not None and y is not None and z is not None: xmin, ymin, xmax, ymax = tile_def.get_tile_meters(x, y, z) From 002f5d46b3cd9fb2c091b9ddeae6aa2e7b31d246 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 13 Apr 2021 17:14:02 -0300 Subject: [PATCH 02/53] add point_aggregation function docstring --- mapshader/core.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 4d5c056..130edb1 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -94,6 +94,20 @@ def create_agg(source: MapSource, def point_aggregation(cvs, data, xfield, yfield, zfield, agg_func): + """ + Compute a reduction by pixel, mapping data to pixels as points. + + Parameters + ---------- + cvs : datashader.Canvas + The input canvas. + data : pandas.DataFrame, dask.DataFrame, or xarray.DataArray/Dataset + The input datasource. + xfield, yfield, zfield : str + Column names for the x, y, and z coordinates of each point. + agg_func : Reduction, optional + Reduction to compute. Default is ``count()``. + """ if zfield: return cvs.points(data, xfield, yfield, getattr(ds, agg_func)(zfield)) else: From d494b1e7c7c1e4d03f11ea009c530c5aced75578 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 13 Apr 2021 17:18:37 -0300 Subject: [PATCH 03/53] add line_aggregation function docstring --- mapshader/core.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 130edb1..5ab9e48 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -115,6 +115,20 @@ def point_aggregation(cvs, data, xfield, yfield, zfield, agg_func): def line_aggregation(cvs, data, zfield, agg_func): + """ + Compute a reduction by pixel, mapping data to pixels as one or more lines. + + Parameters + ---------- + cvs : datashader.Canvas + The input canvas. + data : pandas.DataFrame, dask.DataFrame, or xarray.DataArray/Dataset + The input datasource. + zfield : str + Column names for z coordinate of each point. + agg_func : Reduction, optional + Reduction to compute. Default is ``any()``. + """ if zfield: return cvs.line(data, geometry='geometry', From dc1ca427908ab8482ad537ffc0387d385d8a7454 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 13 Apr 2021 17:21:47 -0300 Subject: [PATCH 04/53] add polygon_aggregation function docstring --- mapshader/core.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 5ab9e48..a9d9c80 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -138,6 +138,21 @@ def line_aggregation(cvs, data, zfield, agg_func): def polygon_aggregation(cvs, data, zfield, agg_func): + """ + Compute a reduction by pixel, mapping data to pixels as one or + more filled polygons. + + Parameters + ---------- + cvs : datashader.Canvas + The input canvas. + data : pandas.DataFrame, dask.DataFrame, or xarray.DataArray/Dataset + The input datasource. + zfield : str + Column names for z coordinate of each point. + agg_func : Reduction, optional + Reduction to compute. Default is ``any()``. + """ if zfield: return cvs.polygons(data, 'geometry', From bf4ae8a5db6b593f40c46b94a173f39adfe37ae4 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 13 Apr 2021 18:02:09 -0300 Subject: [PATCH 05/53] add get_data_array_extent function docstring --- mapshader/transforms.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index e153895..328f3b8 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -59,6 +59,20 @@ def orient_array(arr): return arr def get_data_array_extent(dataarray): + """ + Get the coordinate of the lower left corner and the coordinate of + the upper right corner in map units. + + Parameters + ---------- + dataarray : xarray.DataArray + The input datasource. + + Returns + ------- + extent : tuple of integers + A tuple of ``(xmin, ymin, xmax, ymax)`` values. + """ return (dataarray.coords['x'].min().item(), dataarray.coords['y'].min().item(), dataarray.coords['x'].max().item(), From 09e2ac6c39adb972960520742ec58cdb14dab120 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 09:50:28 -0300 Subject: [PATCH 06/53] add raster_aggregation function docstring --- mapshader/core.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index a9d9c80..9da4520 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -169,6 +169,22 @@ def get_data_array_extent(dataarray): def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd.max()): + """ + Sample a raster dataset by canvas size and bounds. + + Parameters + ---------- + cvs : datashader.Canvas + The input canvas. + data : pandas.DataFrame, dask.DataFrame, or xarray.DataArray/Dataset + The input datasource. + interpolate : str, default=linear + Resampling mode when upsampling raster. + Options include: nearest, linear. + padding : int, default=0 + The padding to be added over the coordinates bounds range. + + """ xmin, xmax = cvs.x_range ymin, ymax = cvs.y_range xdrange = (xmax - xmin) * (1 + 2 * padding) From 48607958db10917346222a5dccc4f5108efae791 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 09:53:30 -0300 Subject: [PATCH 07/53] add default values on create_agg function docstring --- mapshader/core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mapshader/core.py b/mapshader/core.py index 9da4520..9a6d717 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -53,8 +53,10 @@ def create_agg(source: MapSource, Y-axis maximum range. x, y, z : float The coordinates to be used to get the bounds inclusive space along the axis. - width, height : int, optional - Width and height of the output aggregate in pixels. + height : int, default=256 + Height of the output aggregate in pixels. + width : int, default=256 + Width of the output aggregate in pixels. """ if x is not None and y is not None and z is not None: From d7736b11a5b06485ccce4f27a4aa79b8fb0504ea Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 10:00:46 -0300 Subject: [PATCH 08/53] add raster_aggregation function docstring --- mapshader/core.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mapshader/core.py b/mapshader/core.py index 9a6d717..0bda889 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -185,7 +185,15 @@ def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd Options include: nearest, linear. padding : int, default=0 The padding to be added over the coordinates bounds range. - + agg : Reduction, default=datashader.reductions.max() + Resampling mode when downsampling raster. The supported + options include: first, last, mean, mode, var, std, min, + The agg can be specified as either a string name or as a + reduction function, but note that the function object will + be used only to extract the agg type (mean, max, etc.) and + the optional column name; the hardcoded raster code + supports only a fixed set of reductions and ignores the + actual code of the provided agg. """ xmin, xmax = cvs.x_range ymin, ymax = cvs.y_range From 80d705d52d61cc68152af1c59b17bc1da2510967 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 10:27:13 -0300 Subject: [PATCH 09/53] add create_agg function return in the docstring --- mapshader/core.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 0bda889..f8e84c5 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -57,6 +57,11 @@ def create_agg(source: MapSource, Height of the output aggregate in pixels. width : int, default=256 Width of the output aggregate in pixels. + + Returns + ------- + agg : xarray.DataArray + The transformed datasource. """ if x is not None and y is not None and z is not None: From e9dbec2926ec5fadb5fa55ee60cacb6718d7d9b3 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 10:29:43 -0300 Subject: [PATCH 10/53] add functions return in the docstring --- mapshader/core.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index f8e84c5..b9b5b09 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -114,6 +114,11 @@ def point_aggregation(cvs, data, xfield, yfield, zfield, agg_func): Column names for the x, y, and z coordinates of each point. agg_func : Reduction, optional Reduction to compute. Default is ``count()``. + + Returns + ------- + agg : xarray.DataArray + The transformed datasource. """ if zfield: return cvs.points(data, xfield, yfield, getattr(ds, agg_func)(zfield)) @@ -135,6 +140,11 @@ def line_aggregation(cvs, data, zfield, agg_func): Column names for z coordinate of each point. agg_func : Reduction, optional Reduction to compute. Default is ``any()``. + + Returns + ------- + agg : xarray.DataArray + The transformed datasource. """ if zfield: return cvs.line(data, @@ -159,6 +169,11 @@ def polygon_aggregation(cvs, data, zfield, agg_func): Column names for z coordinate of each point. agg_func : Reduction, optional Reduction to compute. Default is ``any()``. + + Returns + ------- + agg : xarray.DataArray + The transformed datasource. """ if zfield: return cvs.polygons(data, @@ -199,6 +214,11 @@ def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd the optional column name; the hardcoded raster code supports only a fixed set of reductions and ignores the actual code of the provided agg. + + Returns + ------- + agg : xarray.DataArray + The transformed datasource. """ xmin, xmax = cvs.x_range ymin, ymax = cvs.y_range From 11ddfc700a593ebe929264539f1217a24aa72561 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 10:30:17 -0300 Subject: [PATCH 11/53] fix raster_aggregation function docstring --- mapshader/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mapshader/core.py b/mapshader/core.py index b9b5b09..cda4da8 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -205,7 +205,7 @@ def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd Options include: nearest, linear. padding : int, default=0 The padding to be added over the coordinates bounds range. - agg : Reduction, default=datashader.reductions.max() + agg_method : Reduction, default=datashader.reductions.max() Resampling mode when downsampling raster. The supported options include: first, last, mean, mode, var, std, min, The agg can be specified as either a string name or as a From 6cca26cfe09be922c0d266371560de2f5b73fa21 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 10:35:18 -0300 Subject: [PATCH 12/53] add apply_additional_transforms function docstring --- mapshader/core.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index cda4da8..818544d 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -258,6 +258,24 @@ def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd 'quantile': quantile} def apply_additional_transforms(source: MapSource, agg: xr.DataArray): + """ + Apply additional transforms over the data, which options could be + `hillshade` or `quantile`. + + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + agg : xarray.DataArray + The transformed datasource. + + Returns + ------- + source : mapshader.sources.MapSource + The input datasource. + agg : xarray.DataArray + The newly transformed datasource. + """ agg = agg.astype('float64') agg.data[agg.data == 0] = np.nan for e in source.extras: From ac36992849aece315dcbc0079cc1332575c2cd85 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 11:55:52 -0300 Subject: [PATCH 13/53] add shade_discrete function docstring --- mapshader/core.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 818544d..573e774 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -290,6 +290,30 @@ def apply_additional_transforms(source: MapSource, agg: xr.DataArray): def shade_discrete(agg, color_key, name='shaded', alpha=255, nodata=0): + """ + Convert a DataArray to an image by choosing an RGBA pixel color + for each value. + + Parameters + ---------- + agg : xarray.DataArray + The input datasource. + color_key : dict + Categories colors. + name : str, default=shaded + Name of the datasource array. + alpha : int, default=255 + Value between 0 - 255 representing the alpha value to use for + colormapped pixels that contain data. + nodata : int, default=0 + The maximum data value, all the values less than this will be + replaced with 0. + + Returns + ------- + img : xarray.DataArray + A DataArray representing an image. + """ if not agg.ndim == 2: raise ValueError("agg must be 2D") From 142b48f5cfba93e5309cbb59851c6c0d9de85392 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 14 Apr 2021 13:51:57 -0300 Subject: [PATCH 14/53] change source definition on docstrings --- mapshader/core.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mapshader/core.py b/mapshader/core.py index 573e774..675fc60 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -42,7 +42,7 @@ def create_agg(source: MapSource, Parameters ---------- source : mapshader.sources.MapSource - The input datasource. + The map source object. xmin : float X-axis minimum range. ymin : float @@ -265,14 +265,14 @@ def apply_additional_transforms(source: MapSource, agg: xr.DataArray): Parameters ---------- source : mapshader.sources.MapSource - The input datasource. + The map source object. agg : xarray.DataArray The transformed datasource. Returns ------- source : mapshader.sources.MapSource - The input datasource. + The map source object. agg : xarray.DataArray The newly transformed datasource. """ @@ -314,7 +314,6 @@ def shade_discrete(agg, color_key, name='shaded', alpha=255, nodata=0): img : xarray.DataArray A DataArray representing an image. """ - if not agg.ndim == 2: raise ValueError("agg must be 2D") From afc267d3097062643fea80fc569a70b6d606fed4 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 16:51:23 -0300 Subject: [PATCH 15/53] add shade_agg function docstring --- mapshader/core.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mapshader/core.py b/mapshader/core.py index 675fc60..320eeaa 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -292,7 +292,7 @@ def apply_additional_transforms(source: MapSource, agg: xr.DataArray): def shade_discrete(agg, color_key, name='shaded', alpha=255, nodata=0): """ Convert a DataArray to an image by choosing an RGBA pixel color - for each value. + for each value by discrete approach. Parameters ---------- @@ -358,6 +358,30 @@ def shade_discrete(agg, color_key, name='shaded', alpha=255, nodata=0): def shade_agg(source: MapSource, agg: xr.DataArray, xmin, ymin, xmax, ymax): + """ + Convert a DataArray to an image by choosing an RGBA pixel color + for each value. + + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + agg : xarray.DataArray + The input datasource. + xmin : float + X-axis minimum range. + ymin : float + Y-axis minimum range. + xmax : float + X-axis maximum range. + ymax : float + Y-axis maximum range. + + Returns + ------- + img : xarray.DataArray + A DataArray representing an image. + """ df = source.data zfield = source.zfield geometry_type = source.geometry_type From 153b4e2a0c6ec4a798a445971c36c499b2b84da2 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:08:30 -0300 Subject: [PATCH 16/53] add to_raster function docstring --- mapshader/core.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 320eeaa..806a0a8 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -421,7 +421,26 @@ def to_raster(source: MapSource, xmin: float = None, ymin: float = None, xmax: float = None, ymax: float = None, height: int = None, width: int = None): + """ + Export a MapSource object to a raster object. + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + xmin : float + X-axis minimum range. + ymin : float + Y-axis minimum range. + xmax : float + X-axis maximum range. + ymax : float + Y-axis maximum range. + height : int + Height of the output aggregate in pixels. + width : int + Width of the output aggregate in pixels. + """ if height is None and width is None: width = 1000 From a91d280f7e428d95ce8b9ac80e4e3c8bcded4f48 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:10:31 -0300 Subject: [PATCH 17/53] add render_map function docstring --- mapshader/core.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 806a0a8..cbe8f53 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -484,7 +484,28 @@ def render_map(source: MapSource, x: float = None, y: float = None, z: float = None, height: int = None, width: int = None, ): + """ + Export a MapSource object to a map object. + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + xmin : float + X-axis minimum range. + ymin : float + Y-axis minimum range. + xmax : float + X-axis maximum range. + ymax : float + Y-axis maximum range. + x, y, z : float + The coordinates to be used to get the bounds inclusive space along the axis. + height : int + Height of the output aggregate in pixels. + width : int + Width of the output aggregate in pixels. + """ if x is not None and y is not None and z is not None: xmin, ymin, xmax, ymax = tile_def.get_tile_meters(x, y, z) From 7d9428ec89a1b7f09f642d453ef3a36518ffb511 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:16:32 -0300 Subject: [PATCH 18/53] add get_geojson function docstring --- mapshader/core.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index cbe8f53..eab4af0 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -561,7 +561,17 @@ def render_map(source: MapSource, def get_geojson(source: MapSource, simplify=None): + """ + Export a MapSource object to a geojson object. + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + simplify : int, default=None + Get the simplified representation of each geometry according + to the toleranced distance. + """ if isinstance(source.data, spatialpandas.GeoDataFrame): gdf = source.data.to_geopandas() From 541c79dcdd46394d2c8783160283dba42c5c7d23 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:18:26 -0300 Subject: [PATCH 19/53] add get_legend function docstring --- mapshader/core.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index eab4af0..6bc74c4 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -592,6 +592,14 @@ def get_geojson(source: MapSource, simplify=None): def get_legend(source: MapSource): + """ + Get the MapSource legend. + + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + """ if source.legend is not None: return source.legend return [] From f7447dc9f92d7e64c583db7ad7e8b8450d39e9df Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:21:07 -0300 Subject: [PATCH 20/53] add render_geojson function docstring --- mapshader/core.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 6bc74c4..4550de4 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -606,6 +606,17 @@ def get_legend(source: MapSource): def render_geojson(source: MapSource, simplify=None): + """ + Export a MapSource object to a geojson object. + + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + simplify : int, default=None + Get the simplified representation of each geometry according + to the toleranced distance. + """ geojson = get_geojson(source, simplify) if isinstance(geojson, dict): From 5db888f7a61dbab8dbf11dd76761b7dbb60c92e9 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:22:27 -0300 Subject: [PATCH 21/53] add render_legend function docstring --- mapshader/core.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mapshader/core.py b/mapshader/core.py index 4550de4..0f84f70 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -626,4 +626,12 @@ def render_geojson(source: MapSource, simplify=None): def render_legend(source: MapSource): + """ + Get the MapSource legend. + + Parameters + ---------- + source : mapshader.sources.MapSource + The input datasource. + """ return json.dumps(render_legend(source)) From 29045766654e498e53656e94c5c217c9aa2c4348 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:34:05 -0300 Subject: [PATCH 22/53] add load_raster function docstring --- mapshader/io.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mapshader/io.py b/mapshader/io.py index 92ae6d3..7cc5746 100644 --- a/mapshader/io.py +++ b/mapshader/io.py @@ -12,6 +12,24 @@ def load_raster(file_path, xmin=None, ymin=None, xmax=None, ymax=None, chunks=None, layername='data'): + """ + Load raster data. + + Parameters + ---------- + file_path : str + Relative path to the file. + xmin : float + X-axis minimum range. + ymin : float + Y-axis minimum range. + xmax : float + X-axis maximum range. + ymax : float + Y-axis maximum range. + layername : str, default=data + Data layer name. + """ if file_path.endswith('.tif'): From ddd95148909a53d29a7601c3e16db840ba3e314d Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:38:08 -0300 Subject: [PATCH 23/53] add load_raster function docstring --- mapshader/io.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mapshader/io.py b/mapshader/io.py index 7cc5746..7f0eee4 100644 --- a/mapshader/io.py +++ b/mapshader/io.py @@ -29,6 +29,11 @@ def load_raster(file_path, xmin=None, ymin=None, Y-axis maximum range. layername : str, default=data Data layer name. + + Returns + ------- + arr : xarray.DataArray + The loaded data. """ if file_path.endswith('.tif'): From 86260a4a87a9b410fe907019538a9462abac388c Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:40:59 -0300 Subject: [PATCH 24/53] add load_vector function docstring --- mapshader/io.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mapshader/io.py b/mapshader/io.py index 7f0eee4..6627158 100644 --- a/mapshader/io.py +++ b/mapshader/io.py @@ -63,4 +63,17 @@ def load_raster(file_path, xmin=None, ymin=None, def load_vector(filepath: str): + """ + Load vector data. + + Parameters + ---------- + filepath : str + Relative path to the file. + + Returns + ------- + gpd : geopandas.DataFrame + The loaded data. + """ return gpd.read_file(filepath) From 5c621eb1c0a18b2ce03e413fcf45bff3ee598a0f Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 20 Apr 2021 17:57:10 -0300 Subject: [PATCH 25/53] add MercatorTileDefinition class docstring --- mapshader/mercator.py | 47 +++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/mapshader/mercator.py b/mapshader/mercator.py index 0f2e7b4..2fb6088 100644 --- a/mapshader/mercator.py +++ b/mapshader/mercator.py @@ -1,40 +1,47 @@ import math def invert_y_tile(y, z): - # Convert from TMS to Google tile y coordinate, and vice versa + """ + Convert from TMS to Google tile y coordinate, and vice versa + """ return (2 ** z) - 1 - y # TODO: change name from source to definition class MercatorTileDefinition(object): - ''' Implementation of mercator tile source - In general, tile sources are used as a required input for ``TileRenderer``. + """ + Implementation of mercator tile source. In general, tile sources + are used as a required input for ``TileRenderer``. + Parameters ---------- x_range : tuple - full extent of x dimension in data units + Full extent of x dimension in data units. y_range : tuple - full extent of y dimension in data units + Full extent of y dimension in data units. max_zoom : int - A maximum zoom level for the tile layer. This is the most zoomed-in level. + A maximum zoom level for the tile layer. This is the most + zoomed-in level. min_zoom : int - A minimum zoom level for the tile layer. This is the most zoomed-out level. + A minimum zoom level for the tile layer. This is the most + zoomed-out level. max_zoom : int - A maximum zoom level for the tile layer. This is the most zoomed-in level. + A maximum zoom level for the tile layer. This is the most + zoomed-in level. x_origin_offset : int - An x-offset in plot coordinates. + An x-offset in plot coordinates. y_origin_offset : int - An y-offset in plot coordinates. + An y-offset in plot coordinates. initial_resolution : int - Resolution (plot_units / pixels) of minimum zoom level of tileset - projection. None to auto-compute. + Resolution (plot_units / pixels) of minimum zoom level of + tileset projection. None to auto-compute. format : int - An y-offset in plot coordinates. + An y-offset in plot coordinates. + Output ------ - tileScheme: MercatorTileSource - ''' - + tileScheme: MercatorTileDefinition + """ def __init__(self, x_range, y_range, tile_size=256, min_zoom=0, max_zoom=30, x_origin_offset=20037508.34, y_origin_offset=20037508.34, initial_resolution=156543.03392804097): @@ -49,16 +56,16 @@ def __init__(self, x_range, y_range, tile_size=256, min_zoom=0, max_zoom=30, self._resolutions = [self._get_resolution(z) for z in range(self.min_zoom, self.max_zoom+1)] def to_ogc_tile_metadata(self, output_file_path): - ''' + """" Create OGC tile metadata XML - ''' + """" pass def to_esri_tile_metadata(self, output_file_path): - ''' + """" Create ESRI tile metadata JSON - ''' + """" pass From 61e72b77732fdc33c3909d6dfefb289f6993bcc2 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 13:45:57 -0300 Subject: [PATCH 26/53] add MapSource class docstring --- mapshader/sources.py | 80 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/mapshader/sources.py b/mapshader/sources.py index 1a3ac7f..f017506 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -15,6 +15,82 @@ class MapSource(object): + """ + This class represents a map source object. + + Parameters + ---------- + name : str + Service name. + description : str + Service description. + filepath : str + Relative path to the data file. + legend : list of dict + Service legend, which could be defined the name, color, value, + and category. + config_path : str + Relative path to the config file. + data : geopandas.GeoDataFrame + Service source data. + geometry_type : str + Geometry type. + key : str + Service route root. + text : str + The service introduction text. + fields : list of str + The geometry fields. + span : str or tuple of int; + Min and max data values to use for colormap/alpha interpolation + when wishing to override autoranging. + geometry_field : str, default=geometry + The geometry field name. + xfield : str, default=geometry + The x field name. + yfield : str, default=geometry + The y field name. + zfield : str + The z field name. + agg_func : str + Reduction to compute. + raster_interpolate : str, default=linear + Resampling mode when upsampling raster. + Options include: nearest, linear. + shade_how : str, default=linear + The interpolation method to use. Valid strings are 'eq_hist', + 'cbrt', 'log', and 'linear'. + cmap : list of colors or matplotlib.colors.Colormap, default=viridis + The colormap to use for 2D agg arrays. + color_key : dict or iterable + The colors to use for a 3D (categorical) agg array. + dynspread : int + The maximum number of pixels to spread on all shape sides. + extras : list of str + The additional transforms over the data, which options could be + 'hillshade' or 'quantile'. + raster_padding : int, default=0 + The padding to be added over the coordinates bounds range. + service_types : list of str + The service types, which options could be 'tile', 'image', + 'wms', and 'geojson'. + full_extent : tuple of int + The coordinate of the lower left corner and the coordinate of + the upper right corner in map units. + default_extent : list of int + The service starting extent. + default_height : int, default=256 + Height of the output aggregate in pixels. + default_width : int, default=256 + Width of the output aggregate in pixels. + overviews : dict + The factors and values to be used when reducing the data + resolution. + transforms : list of dict + The transforms to be applied over the data. + preload : bool, default=False + Preload the data after the service started. + """ def __init__(self, name=None, @@ -134,7 +210,9 @@ def get_full_extent(self): raise NotImplementedError() def load(self): - + """ + Load the service data. + """ if self.is_loaded: return self From b62848c7020d1070e658542c628810df58478e54 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 13:46:16 -0300 Subject: [PATCH 27/53] fix MercatorTileDefinition class docstring --- mapshader/mercator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mapshader/mercator.py b/mapshader/mercator.py index 2fb6088..f01d3c4 100644 --- a/mapshader/mercator.py +++ b/mapshader/mercator.py @@ -56,16 +56,16 @@ def __init__(self, x_range, y_range, tile_size=256, min_zoom=0, max_zoom=30, self._resolutions = [self._get_resolution(z) for z in range(self.min_zoom, self.max_zoom+1)] def to_ogc_tile_metadata(self, output_file_path): - """" + """ Create OGC tile metadata XML - """" + """ pass def to_esri_tile_metadata(self, output_file_path): - """" + """ Create ESRI tile metadata JSON - """" + """ pass From ab47183e59a9b070ca9be686762913edc7f94774 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 13:52:57 -0300 Subject: [PATCH 28/53] add RasterSource and VectorSource class docstring --- mapshader/sources.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mapshader/sources.py b/mapshader/sources.py index f017506..9801318 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -298,6 +298,14 @@ def from_obj(obj: dict): class RasterSource(MapSource): + """ + This class represents a raster source object. + + Parameters + ---------- + MapSource : mapshader.sources.MapSource + The map source object. + """ @property def load_func(self): @@ -313,6 +321,14 @@ def full_extent(self): class VectorSource(MapSource): + """ + This class represents a vector source object. + + Parameters + ---------- + MapSource : mapshader.sources.MapSource + The map source object. + """ @property def load_func(self): From 9b73002617b794783551e8517dc9596a1d868c47 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 14:15:31 -0300 Subject: [PATCH 29/53] add service classes docstring --- mapshader/sources.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/mapshader/sources.py b/mapshader/sources.py index 9801318..dd7e4cf 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -344,6 +344,14 @@ def full_extent(self): class MapService(): + """ + This class represents a map service object. + + Parameters + ---------- + MapSource : mapshader.sources.MapSource + The map source object. + """ def __init__(self, source: MapSource, renderers=[]): self.source = source @@ -351,38 +359,65 @@ def __init__(self, source: MapSource, renderers=[]): @property def key(self): + """ + Get the route before the coordinates. + """ return f'{self.source.key}-{self.service_type}' @property def name(self): + """ + Get the service name and type. + """ return f'{self.source.name} {self.service_type}' @property def legend_name(self): + """ + Get the legend name. + """ return f'{self.name}-legend' @property def default_extent(self): + """ + Get the default extent. + """ return self.source.default_extent @property def default_width(self): + """ + Get the default width. + """ return self.source.default_width @property def default_height(self): + """ + Get the default height. + """ return self.source.default_height @property def service_page_url(self): + """ + Get the service page url. + """ return f'/{self.key}' @property def legend_url(self): + """ + Get the legend url. + """ return f'/{self.key}/legend' @property def service_page_name(self): + """ + Get the service page name. + """ return f'/{self.key}-{self.service_type}' @property @@ -403,6 +438,9 @@ def service_type(self): class TileService(MapService): + """ + This class represents a tile service object. + """ @property def service_url(self): @@ -422,6 +460,9 @@ def service_type(self): class ImageService(MapService): + """ + This class represents a image service object. + """ @property def service_url(self): @@ -450,6 +491,9 @@ def service_type(self): return 'image' class WMSService(MapService): + """ + This class represents a WMS service object. + """ @property def service_url(self): @@ -479,6 +523,9 @@ def service_type(self): class GeoJSONService(MapService): + """ + This class represents a GeoJSON service object. + """ @property def service_url(self): From 3cb20d5df690017ffb15df3b005c729299fce6b8 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 14:25:50 -0300 Subject: [PATCH 30/53] fix indentation --- mapshader/sources.py | 54 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/mapshader/sources.py b/mapshader/sources.py index dd7e4cf..ad258e6 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -359,65 +359,65 @@ def __init__(self, source: MapSource, renderers=[]): @property def key(self): - """ - Get the route before the coordinates. - """ + """ + Get the route before the coordinates. + """ return f'{self.source.key}-{self.service_type}' @property def name(self): - """ - Get the service name and type. - """ + """ + Get the service name and type. + """ return f'{self.source.name} {self.service_type}' @property def legend_name(self): - """ - Get the legend name. - """ + """ + Get the legend name. + """ return f'{self.name}-legend' @property def default_extent(self): - """ - Get the default extent. - """ + """ + Get the default extent. + """ return self.source.default_extent @property def default_width(self): - """ - Get the default width. - """ + """ + Get the default width. + """ return self.source.default_width @property def default_height(self): - """ - Get the default height. - """ + """ + Get the default height. + """ return self.source.default_height @property def service_page_url(self): - """ - Get the service page url. - """ + """ + Get the service page url. + """ return f'/{self.key}' @property def legend_url(self): - """ - Get the legend url. - """ + """ + Get the legend url. + """ return f'/{self.key}/legend' @property def service_page_name(self): - """ - Get the service page name. - """ + """ + Get the service page name. + """ return f'/{self.key}-{self.service_type}' @property From da98c371d53cd1d49857342165b48aea4e84e24e Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 14:47:52 -0300 Subject: [PATCH 31/53] add parse_sources function docstring --- mapshader/sources.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mapshader/sources.py b/mapshader/sources.py index ad258e6..0a7474f 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -763,7 +763,19 @@ def elevation_source_netcdf(): def parse_sources(source_objs, config_path=None, contains=None): + """ + Parse ``mapshader.sources.MapSource`` and instantiate a + ``mapshader.sources.MapService``. + Parameters + ---------- + source_objs : list of ``mapshader.sources.MapSource`` + The map source objects. + config_path : str + Relative path to the config file. + contains : str + Skip the service type creation that contains this route. + """ service_classes = { 'tile': TileService, 'wms': WMSService, From e7f1e590036db6c78e1e22c56aeb5f8232211a5a Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 14:53:32 -0300 Subject: [PATCH 32/53] add get_services function docstring --- mapshader/sources.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mapshader/sources.py b/mapshader/sources.py index 0a7474f..41fb1bd 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -801,6 +801,20 @@ def parse_sources(source_objs, config_path=None, contains=None): def get_services(config_path=None, include_default=True, contains=None, sources=None): + """ + Get the map services. + + Parameters + ---------- + config_path : str + Relative path to the config file. + include_default : bool, default=True + Include demo services. + contains : str + Skip the service type creation that contains this route. + sources : list of ``mapshader.sources.MapSource`` + The map source objects. + """ source_objs = None From e74734c92e98d078e5e0e68d02c69fc2dc6c7eaf Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 14:59:46 -0300 Subject: [PATCH 33/53] add reproject_raster function docstring --- mapshader/transforms.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 328f3b8..a98ec48 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -22,6 +22,21 @@ } def reproject_raster(arr: xr.DataArray, epsg=3857): + """ + Reproject raster data. + + Parameters + ---------- + arr : xarray.DataArray + The raster data. + epsg : str + The coordinate systems code. + + Returns + ------- + reproj_data : xarray.DataArray + The reprojected data. + """ global projections try: From 1f003247232f374f69afa64e1f38291357517243 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:01:51 -0300 Subject: [PATCH 34/53] add reproject_vector function docstring --- mapshader/transforms.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index a98ec48..183d913 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -29,7 +29,7 @@ def reproject_raster(arr: xr.DataArray, epsg=3857): ---------- arr : xarray.DataArray The raster data. - epsg : str + epsg : int The coordinate systems code. Returns @@ -47,6 +47,21 @@ def reproject_raster(arr: xr.DataArray, epsg=3857): def reproject_vector(gdf: gpd.GeoDataFrame, epsg=3857): + """ + Reproject vector data. + + Parameters + ---------- + gdf : geopandas.GeoDataFrame + The vector data. + epsg : int + The coordinate systems code. + + Returns + ------- + reproj_data : geopandas.GeoDataFrame + The reprojected data. + """ return gdf.to_crs(epsg=epsg) From 0874bac8195c966b58f2d4d25044d991e7d7bd8c Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:06:19 -0300 Subject: [PATCH 35/53] add flip_coords function docstring --- mapshader/transforms.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 183d913..7fe0daf 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -66,6 +66,21 @@ def reproject_vector(gdf: gpd.GeoDataFrame, epsg=3857): def flip_coords(arr, dim): + """ + Flip the geometry coordinates. + + Parameters + ---------- + arr : xarray.DataArray + The data source. + dim : str + The coordinate field name. + + Returns + ------- + flipped_data : xarray.DataArray + The flipped coordinates data. + """ args = {dim: list(reversed(arr.coords[dim]))} arr = arr.assign_coords(**args) return arr From f22d0165a30c51d03a652f46dd53884b6cbfddd9 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:17:19 -0300 Subject: [PATCH 36/53] add to_spatialpandas function docstring --- mapshader/transforms.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 7fe0daf..f9ccbaa 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -87,6 +87,9 @@ def flip_coords(arr, dim): def to_spatialpandas(gdf: gpd.GeoDataFrame, geometry_field='geometry'): + """ + Convert a ``geopandas.GeoDataFrame`` to ``spatialpandas.GeoDataFrame``. + """ return spatialpandas.GeoDataFrame(gdf, geometry=geometry_field) From 8a15eb13f2ccbac79ed583ef1f0848f749967dcc Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:18:26 -0300 Subject: [PATCH 37/53] add squeeze function docstring --- mapshader/transforms.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index f9ccbaa..e3d0708 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -94,6 +94,21 @@ def to_spatialpandas(gdf: gpd.GeoDataFrame, geometry_field='geometry'): def squeeze(arr, dim): + """ + Return a new ``xarray.DataArray`` with squeezed data. + + Parameters + ---------- + arr : xarray.DataArray + The data source. + dim : str + Drop a specific field name. + + Returns + ------- + squeezed_data : xarray.DataArray + The squeezed data. + """ return arr.squeeze().drop(dim) From 4326d963544df3ec3460622582d90ae8d9c96074 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:20:43 -0300 Subject: [PATCH 38/53] add cast function docstring --- mapshader/transforms.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index e3d0708..a8bcfb9 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -113,6 +113,21 @@ def squeeze(arr, dim): def cast(arr, dtype): + """ + Cast the data to a specific data type. + + Parameters + ---------- + arr : xarray.DataArray + The data source. + dtype : str + Data type. + + Returns + ------- + casted_data : xarray.DataArray + The casted data. + """ arr.data = arr.data.astype(dtype) return arr From 3d92665adcc315de6bd1566b902e99d12597d9d7 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:23:15 -0300 Subject: [PATCH 39/53] add orient_array function docstring --- mapshader/transforms.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index a8bcfb9..ffc5ac4 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -133,6 +133,20 @@ def cast(arr, dtype): def orient_array(arr): + """ + Reorients the array to a canonical orientation depending on + whether the x and y-resolution values are positive or negative. + + Parameters + ---------- + arr : xarray.DataArray + Data to be reoriented + + Returns + ------- + reoriented_data : numpy.ndarray + Reoriented 2d NumPy ndarray + """ arr.data = ds.utils.orient_array(arr) return arr From bc03484d1a891bb5b5a9b0c06d0fd7df932fd4f1 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 15:27:43 -0300 Subject: [PATCH 40/53] add canvas_like function docstring --- mapshader/transforms.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index ffc5ac4..e414011 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -171,6 +171,19 @@ def get_data_array_extent(dataarray): dataarray.coords['y'].max().item()) def canvas_like(dataarray): + """ + Get a ``datashader.Canvas`` according to a ``xarray.DataArray`` features. + + Parameters + ---------- + dataarray : xarray.DataArray + The input datasource. + + Returns + ------- + canvas : datashader.Canvas + An abstract canvas representing the space in which to bin. + """ if isinstance(dataarray, xr.DataArray): extent = get_data_array_extent(dataarray) From 6f2ff3cfc43aadbc4d3ebe7764e5dad41182c424 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 17:34:06 -0300 Subject: [PATCH 41/53] add build_vector_overviews function docstring --- mapshader/transforms.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index e414011..063fa3b 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -200,6 +200,24 @@ def canvas_like(dataarray): def build_vector_overviews(gdf, levels, geometry_field='geometry'): + """ + Reduce the vector data resolution. + + Parameters + ---------- + gdf : geopandas.GeoDataFrame + The vector data. + levels : dict + The factors and values to be used when reducing the data + resolution. + geometry_field : str, default=geometry + The geometry field name. + + Returns + ------- + overviews : geopandas.GeoDataFrame + The reduced resolution vector data. + """ values = {} overviews = {} for level, simplify_tol in levels.items(): From f98435835e81a404508958630238fdf9011535df Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 17:37:30 -0300 Subject: [PATCH 42/53] add build_raster_overviews function docstring --- mapshader/transforms.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 063fa3b..36049b6 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -237,6 +237,25 @@ def build_vector_overviews(gdf, levels, geometry_field='geometry'): def build_raster_overviews(arr, levels, interpolate='linear'): + """ + Reduce the raster data resolution. + + Parameters + ---------- + arr : xarray.DataArray + The raster data. + levels : dict + The factors and values to be used when reducing the data + resolution. + interpolate : str, default=linear + Resampling mode when upsampling raster. + Options include: nearest, linear. + + Returns + ------- + overviews : xarray.DataArray + The reduced resolution raster data. + """ values = {} overviews = {} for level, resolution in levels.items(): From b87adffabb0b490191c3088571514b1945dac232 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 17:38:58 -0300 Subject: [PATCH 43/53] add add_xy_fields function docstring --- mapshader/transforms.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 36049b6..bf59460 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -283,6 +283,9 @@ def build_raster_overviews(arr, levels, interpolate='linear'): def add_xy_fields(gdf, geometry_field='geometry'): + """ + Extract x and y fields from geometry and create new columns with them. + """ gdf['X'] = gdf[geometry_field].apply(lambda p: p.x) gdf['Y'] = gdf[geometry_field].apply(lambda p: p.y) return gdf From 37d0d5bf80ecacf90a2dfd1cea745e2869373022 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 17:52:51 -0300 Subject: [PATCH 44/53] add select_by_attributes function docstring --- mapshader/transforms.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index bf59460..fc9fa9c 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -292,6 +292,27 @@ def add_xy_fields(gdf, geometry_field='geometry'): def select_by_attributes(gdf, field, value, operator='IN'): + """ + Filter a ``geopandas.GeoDataFrame`` data. + + Parameters + ---------- + gdf : geopandas.GeoDataFrame + The vector data. + field : str + Column to be filtered. + value : int or float + Value to compare when filtering. + operator : str, default=IN + Arithmetic operator to be used when filtering. + Options include: IN, NOT IN, EQUALS, NOT EQUALS, LT, GT, LTE, + and GTE. + + Returns + ------- + filtered_data : geopandas.GeoDataFrame + The filtered data. + """ if operator == 'IN': return gdf[gdf[field].isin(value)] From dc3950a1954d46a65972187e50b3fc01c9153012 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 17:56:56 -0300 Subject: [PATCH 45/53] add polygon_to_line function docstring --- mapshader/transforms.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index fc9fa9c..eca26c5 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -340,6 +340,21 @@ def select_by_attributes(gdf, field, value, operator='IN'): def polygon_to_line(gdf, geometry_field='geometry'): + """ + Convert geometry type from polygon to line. + + Parameters + ---------- + gdf : geopandas.GeoDataFrame + The vector data. + geometry_field : str, default=geometry + The geometry field name. + + Returns + ------- + gdf : geopandas.GeoDataFrame + The converted data. + """ gdf[geometry_field] = gdf[geometry_field].boundary return gdf From 196ed5e21c5eb6b76b2bffba688a74d5c702b588 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 18:02:23 -0300 Subject: [PATCH 46/53] add raster_to_categorical_points function docstring --- mapshader/transforms.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index eca26c5..d7dd02f 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -360,6 +360,23 @@ def polygon_to_line(gdf, geometry_field='geometry'): def raster_to_categorical_points(arr, cats: dict, dim: str = 'data'): + """ + Convert raster data to categorical points. + + Parameters + ---------- + arr : xarray.DataArray + The raster data. + cats : dict + Categories colors. + dim : str, default=data + The categorical column name. + + Returns + ------- + df : pandas.DataFrame + The converted data. + """ df = None if isinstance(arr, da.Array): df = arr.compute().to_dataframe() From 047969969ad04043e4d062c40d9470015e970f47 Mon Sep 17 00:00:00 2001 From: giancastro Date: Wed, 21 Apr 2021 18:03:37 -0300 Subject: [PATCH 47/53] add get_transform_by_name function docstring --- mapshader/transforms.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index d7dd02f..56b669f 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -409,4 +409,7 @@ def raster_to_categorical_points(arr, cats: dict, dim: str = 'data'): def get_transform_by_name(name: str): + """ + Get transform function by their name. + """ return _transforms[name] From f3b25eeca5da475f3afa7ac5d498a32d9561f087 Mon Sep 17 00:00:00 2001 From: giancastro Date: Thu, 22 Apr 2021 13:08:24 -0300 Subject: [PATCH 48/53] fix apply_additional_transforms function docstring --- mapshader/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mapshader/core.py b/mapshader/core.py index 0f84f70..8da8f2c 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -260,7 +260,7 @@ def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd def apply_additional_transforms(source: MapSource, agg: xr.DataArray): """ Apply additional transforms over the data, which options could be - `hillshade` or `quantile`. + ``hillshade`` or ``quantile``. Parameters ---------- From aa57177bdd0308540c962e697b50b5a4eecd45ba Mon Sep 17 00:00:00 2001 From: giancastro Date: Thu, 22 Apr 2021 13:18:50 -0300 Subject: [PATCH 49/53] fix MercatorTileDefinition class docstring --- mapshader/mercator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mapshader/mercator.py b/mapshader/mercator.py index f01d3c4..672b14c 100644 --- a/mapshader/mercator.py +++ b/mapshader/mercator.py @@ -38,8 +38,8 @@ class MercatorTileDefinition(object): format : int An y-offset in plot coordinates. - Output - ------ + Returns + ------- tileScheme: MercatorTileDefinition """ def __init__(self, x_range, y_range, tile_size=256, min_zoom=0, max_zoom=30, From 9e9dbbe2b76f7bcd1e84fce719e834fb98a72f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renato=20C=C3=A9sar?= Date: Wed, 2 Jun 2021 09:46:09 -0300 Subject: [PATCH 50/53] Update core raster documentation and transform --- mapshader/core.py | 35 ++++++++++++++++++++++++++--------- mapshader/transforms.py | 11 +++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/mapshader/core.py b/mapshader/core.py index 8da8f2c..695f479 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -198,7 +198,7 @@ def raster_aggregation(cvs, data, interpolate='linear', padding=0, agg_method=rd ---------- cvs : datashader.Canvas The input canvas. - data : pandas.DataFrame, dask.DataFrame, or xarray.DataArray/Dataset + data : xarray.DataArray, xr.Dataset or dask.Array The input datasource. interpolate : str, default=linear Resampling mode when upsampling raster. @@ -560,9 +560,9 @@ def render_map(source: MapSource, return img -def get_geojson(source: MapSource, simplify=None): +def get_source_data(source: MapSource, simplify=None): """ - Export a MapSource object to a geojson object. + Get MapSource data and return as dict or GeoDataFrame depending on the geometry type. Parameters ---------- @@ -571,6 +571,11 @@ def get_geojson(source: MapSource, simplify=None): simplify : int, default=None Get the simplified representation of each geometry according to the toleranced distance. + + Returns + ------- + gdf : GeoDataFrame or dict + The Mapsource data """ if isinstance(source.data, spatialpandas.GeoDataFrame): gdf = source.data.to_geopandas() @@ -599,6 +604,10 @@ def get_legend(source: MapSource): ---------- source : mapshader.sources.MapSource The input datasource. + + Returns + ------- + legend : list """ if source.legend is not None: return source.legend @@ -616,22 +625,30 @@ def render_geojson(source: MapSource, simplify=None): simplify : int, default=None Get the simplified representation of each geometry according to the toleranced distance. + + Returns + ------- + geojson : string """ - geojson = get_geojson(source, simplify) + data = get_source_data(source, simplify) - if isinstance(geojson, dict): - return json.dumps(geojson) + if isinstance(data, dict): + return json.dumps(data) - return geojson.to_json() + return data.to_json() def render_legend(source: MapSource): """ - Get the MapSource legend. + Get the MapSource legend and return as a JSON string. Parameters ---------- source : mapshader.sources.MapSource The input datasource. + + Returns + ------- + geojson : string """ - return json.dumps(render_legend(source)) + return json.dumps(get_legend(source)) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 56b669f..24b06df 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -89,6 +89,17 @@ def flip_coords(arr, dim): def to_spatialpandas(gdf: gpd.GeoDataFrame, geometry_field='geometry'): """ Convert a ``geopandas.GeoDataFrame`` to ``spatialpandas.GeoDataFrame``. + + Parameters + ---------- + gdf : geopandas.GeoDataFrame + The data source. + geometry_field : str + Geometry field on GeoDataFrame + + Returns + ------- + spatial_gdf : spatialpandas.GeoDataFrame """ return spatialpandas.GeoDataFrame(gdf, geometry=geometry_field) From eeccb270ff686af6bf87b43f582250ed19795528 Mon Sep 17 00:00:00 2001 From: Iury Piva Date: Tue, 8 Jun 2021 09:21:01 -0300 Subject: [PATCH 51/53] updated docstrings and added some examples --- mapshader/core.py | 4 ++-- mapshader/sources.py | 8 ++++++-- mapshader/transforms.py | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/mapshader/core.py b/mapshader/core.py index 695f479..ef97c4f 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -37,7 +37,7 @@ def create_agg(source: MapSource, """ Instantiate an abstract canvas representing the space and compute a reduction by pixel according to the geometry type applying the - inputted aggregation function. + aggregation function defined in source. Parameters ---------- @@ -607,7 +607,7 @@ def get_legend(source: MapSource): Returns ------- - legend : list + legend : list of dict """ if source.legend is not None: return source.legend diff --git a/mapshader/sources.py b/mapshader/sources.py index 41fb1bd..266d01a 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -87,7 +87,11 @@ class MapSource(object): The factors and values to be used when reducing the data resolution. transforms : list of dict - The transforms to be applied over the data. + The transforms to be applied over the data, which options could include: + 'reproject_raster', 'reproject_vector', 'orient_array', 'cast', + 'flip_coords', 'build_raster_overviews', 'build_vector_overviews', + 'squeeze', 'to_spatialpandas', 'add_xy_fields', 'select_by_attributes', + 'polygon_to_line', 'raster_to_categorical_points' preload : bool, default=False Preload the data after the service started. """ @@ -367,7 +371,7 @@ def key(self): @property def name(self): """ - Get the service name and type. + Get the source name and service type. """ return f'{self.source.name} {self.service_type}' diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 24b06df..039fc21 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -221,6 +221,8 @@ def build_vector_overviews(gdf, levels, geometry_field='geometry'): levels : dict The factors and values to be used when reducing the data resolution. + + E.g: {'0': 10000, '1': 2500, '2': 1250,...} geometry_field : str, default=geometry The geometry field name. @@ -258,6 +260,8 @@ def build_raster_overviews(arr, levels, interpolate='linear'): levels : dict The factors and values to be used when reducing the data resolution. + + E.g: {'0': 10000, '1': 2500, '2': 1250,...} interpolate : str, default=linear Resampling mode when upsampling raster. Options include: nearest, linear. @@ -422,5 +426,21 @@ def raster_to_categorical_points(arr, cats: dict, dim: str = 'data'): def get_transform_by_name(name: str): """ Get transform function by their name. + + Parameters + ---------- + name : 'reproject_raster' | + 'reproject_vector' | + 'orient_array' | + 'cast' | + 'flip_coords' | + 'build_raster_overviews' | + 'build_vector_overviews' | + 'squeeze' | + 'to_spatialpandas' | + 'add_xy_fields' | + 'select_by_attributes' | + 'polygon_to_line' | + 'raster_to_categorical_points' """ return _transforms[name] From 54136cc05f5333b2ea67e6999c521e67d6c368f0 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 8 Jun 2021 09:41:44 -0300 Subject: [PATCH 52/53] fix --- mapshader/sources.py | 8 ++++---- mapshader/transforms.py | 19 ++----------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/mapshader/sources.py b/mapshader/sources.py index 266d01a..0f46e00 100644 --- a/mapshader/sources.py +++ b/mapshader/sources.py @@ -87,11 +87,11 @@ class MapSource(object): The factors and values to be used when reducing the data resolution. transforms : list of dict - The transforms to be applied over the data, which options could include: - 'reproject_raster', 'reproject_vector', 'orient_array', 'cast', - 'flip_coords', 'build_raster_overviews', 'build_vector_overviews', + The transforms to be applied over the data, which options could + include: 'reproject_raster', 'reproject_vector', 'orient_array', + 'cast', 'flip_coords', 'build_raster_overviews', 'build_vector_overviews', 'squeeze', 'to_spatialpandas', 'add_xy_fields', 'select_by_attributes', - 'polygon_to_line', 'raster_to_categorical_points' + 'polygon_to_line', and 'raster_to_categorical_points'. preload : bool, default=False Preload the data after the service started. """ diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 039fc21..23c1615 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -221,8 +221,6 @@ def build_vector_overviews(gdf, levels, geometry_field='geometry'): levels : dict The factors and values to be used when reducing the data resolution. - - E.g: {'0': 10000, '1': 2500, '2': 1250,...} geometry_field : str, default=geometry The geometry field name. @@ -260,8 +258,6 @@ def build_raster_overviews(arr, levels, interpolate='linear'): levels : dict The factors and values to be used when reducing the data resolution. - - E.g: {'0': 10000, '1': 2500, '2': 1250,...} interpolate : str, default=linear Resampling mode when upsampling raster. Options include: nearest, linear. @@ -429,18 +425,7 @@ def get_transform_by_name(name: str): Parameters ---------- - name : 'reproject_raster' | - 'reproject_vector' | - 'orient_array' | - 'cast' | - 'flip_coords' | - 'build_raster_overviews' | - 'build_vector_overviews' | - 'squeeze' | - 'to_spatialpandas' | - 'add_xy_fields' | - 'select_by_attributes' | - 'polygon_to_line' | - 'raster_to_categorical_points' + name : string + The transform function name. """ return _transforms[name] From 27258acf7a8f8789db9c73a419846fc9fa92c116 Mon Sep 17 00:00:00 2001 From: giancastro Date: Tue, 8 Jun 2021 09:43:38 -0300 Subject: [PATCH 53/53] fix --- mapshader/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mapshader/transforms.py b/mapshader/transforms.py index 23c1615..c184dc5 100644 --- a/mapshader/transforms.py +++ b/mapshader/transforms.py @@ -425,7 +425,7 @@ def get_transform_by_name(name: str): Parameters ---------- - name : string + name : str The transform function name. """ return _transforms[name]