Skip to content

Commit

Permalink
Update to support geopandas with pyproj.CRS (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 authored Mar 10, 2020
1 parent 27620a3 commit 680cb3f
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ before_install:
- conda config --add channels conda-forge
- conda config --set channel_priority strict
# Create conda environment for geocube
- conda create -n test python=$PYTHON_VERSION $CONDA_COMPILER datacube gdal=2.4.* geopandas libgdal=2.4.* rasterio=1.0.* scipy xarray
- conda create -n test python=$PYTHON_VERSION $CONDA_COMPILER datacube gdal=2.4 geopandas libgdal=2.4 rasterio scipy xarray
- source activate test

install:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ install:
#-----------------------------------------------------------------------------
# Create conda environment for geocube
#-----------------------------------------------------------------------------
- conda create -n test python=%PYTHON_VERSION% datacube gdal=2.4.* geopandas libgdal=2.4.* rasterio=1.0.* scipy xarray
- conda create -n test python=%PYTHON_VERSION% datacube gdal=2.4 geopandas libgdal=2.4 rasterio scipy xarray
- activate test

#-------------------------------------------------------------------------------
Expand Down
80 changes: 58 additions & 22 deletions docs/examples/categorical.ipynb

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions docs/examples/resample_point_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
"metadata": {},
"outputs": [],
"source": [
"gdf = gpd.read_file(\"../../test/test_data/input/time_vector_data.geojson\")\n",
"gdf.crs = {'init': 'epsg:4326'}"
"gdf = gpd.read_file(\n",
" \"../../test/test_data/input/time_vector_data.geojson\",\n",
" crs=\"epsg:4326\",\n",
")"
]
},
{
Expand Down Expand Up @@ -388,9 +390,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
50 changes: 31 additions & 19 deletions docs/examples/timestamp_missing_data.ipynb

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
History
=======

0.0.12
------
- ENH: Update to support geopandas with pyproj.CRS (pull #18)
- BUG: Update timestamp handling to ensure correct format for dtype (pull #18)

0.0.11
------
- Drop Python 3.5 Support (issue #12)
Expand Down
15 changes: 14 additions & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ Installation
Stable release
--------------

. Use pip to install from `PyPI <https://pypi.org/project/geocube/>`__:
Use pip to install from `PyPI <https://pypi.org/project/geocube/>`__:


Step 1: Install python GDAL version associated with your GDAL version.

Here is a Linux example with GDAL installed in your system:

.. code-block:: bash
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
pip install GDAL~=$(gdal-config --version | awk -F'[.]' '{print $1"."$2}').0
Step 2: Install from pip:

.. code-block:: bash
Expand Down
2 changes: 1 addition & 1 deletion geocube/cli/commands/make_geocube.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"-c",
"--output-crs",
help=(
"The CRS of the returned data. (e.x. +init=epsg:4326). "
"The CRS of the returned data. (e.g. epsg:4326). "
"If no CRS is supplied, the CRS of the input data is used."
),
required=False,
Expand Down
25 changes: 0 additions & 25 deletions geocube/geo_utils/crs.py

This file was deleted.

18 changes: 8 additions & 10 deletions geocube/geo_utils/geobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import geopandas as gpd
import rioxarray # noqa
from datacube.utils import geometry
from rasterio.crs import CRS
from rioxarray.crs import crs_to_wkt
from shapely.geometry import box, mapping

from geocube.exceptions import VectorDataError
from geocube.geo_utils.crs import crs_to_wkt
from geocube.logger import get_logger


Expand Down Expand Up @@ -71,7 +70,7 @@ def load_vector_data(vector_data):

# make sure projection is set
if not vector_data.crs:
vector_data.crs = {"init": "epsg:4326"}
vector_data.crs = "EPSG:4326"
logger.warning(
"Projection not defined in `vector_data`."
" Setting to geographic (EPSG:4326)."
Expand Down Expand Up @@ -149,9 +148,9 @@ def from_vector(self, vector_data):
raise RuntimeError("Must specify 'resolution' if 'like' not specified.")

if self.output_crs:
crs = geometry.CRS(self.output_crs)
crs = geometry.CRS(crs_to_wkt(self.output_crs))
else:
crs = geometry.CRS(crs_to_wkt(CRS.from_user_input(vector_data.crs)))
crs = geometry.CRS(crs_to_wkt(vector_data.crs))

if self.geom is None and self.output_crs:
geopoly = geometry.Geometry(
Expand All @@ -168,12 +167,11 @@ def from_vector(self, vector_data):
else:
geom_json = json.loads(self.geom)
geom_crs = geometry.CRS(
"+init={}".format(
geom_json["crs"]["properties"]["name"].lower()
if "crs" in geom_json
else "epsg:4326"
)
geom_json["crs"]["properties"]["name"]
if "crs" in geom_json
else "epsg:4326"
)

geopoly = geometry.Geometry(geom_json, crs=geom_crs)

return geometry.GeoBox.from_geopolygon(
Expand Down
2 changes: 0 additions & 2 deletions geocube/rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import numpy
import pandas
import rasterio.features
import rasterio.transform
import rasterio.warp
from rasterio.enums import MergeAlg
from scipy.interpolate import Rbf, griddata
from shapely.geometry import mapping
Expand Down
6 changes: 2 additions & 4 deletions geocube/vector_to_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,13 @@ def make_geocube(
set(datetime_measurements) & set(measurements)
)
# reproject vector data to the projection of the output raster
vector_data = self.vector_data.to_crs(
self.geobox.crs._crs.ExportToProj4()
) # pylint: disable=protected-access
vector_data = self.vector_data.to_crs(self.geobox.crs.wkt)

# convert to datetime
for datetime_measurement in self.datetime_measurements:
vector_data[datetime_measurement] = pandas.to_datetime(
vector_data[datetime_measurement]
)
).astype("datetime64[ns]")

# get categorical enumerations if they exist
self._categorical_enums = {}
Expand Down
5 changes: 3 additions & 2 deletions geocube/xarray_extensions/vectorxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import geopandas as gpd
import xarray
from pyproj import CRS
from shapely.wkb import dumps, loads


Expand All @@ -14,7 +15,7 @@ def from_geodataframe(in_geodataframe):
geodf = in_geodataframe.copy().set_index("geometry")
geox = xarray.Dataset.from_dataframe(geodf)
geox.coords["crs"] = 0
geox.coords["crs"].attrs = geodf.crs
geox.coords["crs"].attrs["crs_wkt"] = CRS.from_user_input(geodf.crs).to_wkt()
return geox


Expand Down Expand Up @@ -60,7 +61,7 @@ def to_geodataframe(self):
if extra_coords:
out_obj = out_obj.copy().reset_coords(extra_coords)
geodf = gpd.GeoDataFrame(out_obj.to_dataframe().reset_index())
geodf.crs = self._obj.coords["crs"].attrs
geodf.crs = self._obj.coords["crs"].attrs["crs_wkt"]
return geodf

def to_netcdf(self, *args, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"appdirs",
"Click>=6.0",
"datacube",
"geopandas",
"geopandas>=0.6",
"rasterio",
"rioxarray",
"rioxarray>=0.0.22",
"xarray",
"pyproj>=2",
]

test_requirements = ["pytest>=3.6", "pytest-cov", "mock"]
Expand Down
10 changes: 5 additions & 5 deletions test/integration/api/test_core_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from test.conftest import TEST_COMPARE_DATA_DIR, TEST_INPUT_DATA_DIR

TEST_GARS_PROJ = "+init=epsg:32615"
TEST_GARS_PROJ = "epsg:32615"
TEST_GARS_POLY = loads(
"POLYGON (("
"-90.58343333333333 41.48343333333334, "
Expand Down Expand Up @@ -257,10 +257,10 @@ def test_make_geocube__convert_time(input_geodata, tmpdir):
@pytest.mark.parametrize(
"load_extra_kwargs",
[
{"output_crs": "+init=epsg:4326"},
{"output_crs": "epsg:4326"},
{"resolution": (-10, 10)},
{"align": (0, 0)},
{"output_crs": "+init=epsg:4326", "resolution": (-10, 10), "align": (0, 0)},
{"output_crs": "epsg:4326", "resolution": (-10, 10), "align": (0, 0)},
],
)
def test_make_geocube__like_error_invalid_args(load_extra_kwargs):
Expand Down Expand Up @@ -588,10 +588,10 @@ def test_make_geocube__group_by_convert_with_time(input_geodata, tmpdir):
@pytest.mark.parametrize(
"load_extra_kwargs",
[
{"output_crs": "+init=epsg:4326"},
{"output_crs": "epsg:4326"},
{"resolution": (-10, 10)},
{"align": (0, 0)},
{"output_crs": "+init=epsg:4326", "resolution": (-10, 10), "align": (0, 0)},
{"output_crs": "epsg:4326", "resolution": (-10, 10), "align": (0, 0)},
],
)
def test_make_geocube__group_by_like_error_invalid_args(load_extra_kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_from_geodataframe():
vxd = vectorxarray.from_geodataframe(gdf)
assert all(gdf.geometry == vxd.geometry.values)
assert sorted(gdf.columns.tolist() + ["crs"]) == sorted(vxd.variables)
assert gdf.crs == vxd.crs.attrs
assert gdf.crs == vxd.crs.attrs["crs_wkt"]
assert "geometry" in vxd.coords
assert "crs" in vxd.coords

Expand Down
Binary file modified test/test_data/compare/rasterize_griddata_cubic.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_griddata_cubic_nodata.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_griddata_nearest.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_griddata_nearest_nodata.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_griddata_rescale.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_image_sum.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_image_sum_nodata.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_radial_linear.nc
Binary file not shown.
Binary file modified test/test_data/compare/rasterize_radial_linear_nodata.nc
Binary file not shown.
7 changes: 2 additions & 5 deletions test/unit/cli/commands/test_make_geocube.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ def _get_called_dict(**kwargs):
["-m", "measure1", "--measurements", "measure2"],
_get_called_dict(measurements=("measure1", "measure2")),
),
(["-c", "+init=epsg:4326"], _get_called_dict(output_crs="+init=epsg:4326")),
(
["--output-crs", "+init=epsg:4326"],
_get_called_dict(output_crs="+init=epsg:4326"),
),
(["-c", "epsg:4326"], _get_called_dict(output_crs="epsg:4326")),
(["--output-crs", "epsg:4326"], _get_called_dict(output_crs="epsg:4326"),),
(["-r", "-10", "10"], _get_called_dict(resolution=(-10, 10))),
(["--resolution", "-10", "10"], _get_called_dict(resolution=(-10, 10))),
(["-a", "-10", "10"], _get_called_dict(align=(-10, 10))),
Expand Down

0 comments on commit 680cb3f

Please sign in to comment.