diff --git a/doc/source/conf.py b/doc/source/conf.py index a9e6430..b266cc9 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -45,7 +45,6 @@ "geopandas": ("https://geopandas.org/en/latest", None), "pandas": ("https://pandas.pydata.org/docs", None), "rasterio": ("https://rasterio.readthedocs.io/en/latest/", None), - } # -- Options for HTML output ------------------------------------------------- @@ -57,9 +56,7 @@ html_logo = "_static/logo.svg" html_favicon = "_static/icon.png" html_theme_options = { - "github_url": "https://github.com/xarray-contrib/xvec", "use_sidenotes": True, - "logo_only": True, } nb_execution_mode = "off" autodoc_typehints = "none" diff --git a/doc/source/extract_pts.ipynb b/doc/source/extract_pts.ipynb index 12c18a2..599d6e3 100644 --- a/doc/source/extract_pts.ipynb +++ b/doc/source/extract_pts.ipynb @@ -421,18 +421,18 @@ " v (month, level, latitude, longitude) float64 ...\n", "Attributes:\n", " Conventions: CF-1.0\n", - " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." + " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." ], "text/plain": [ "\n", @@ -899,10 +899,10 @@ " geometry GeometryIndex (crs=None)\n", "Attributes:\n", " Conventions: CF-1.0\n", - " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    • z
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      5
      units :
      m**2 s**-2
      long_name :
      Geopotential
      standard_name :
      geopotential
      [60 values with dtype=float64]
    • u
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      U component of wind
      standard_name :
      eastward_wind
      [60 values with dtype=float64]
    • v
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      V component of wind
      standard_name :
      northward_wind
      [60 values with dtype=float64]
    • level
      PandasIndex
      PandasIndex(Index([200, 500, 850], dtype='int32', name='level'))
    • month
      PandasIndex
      PandasIndex(Index([1, 7], dtype='int32', name='month'))
    • geometry
      GeometryIndex (crs=None)
      <xvec.index.GeometryIndex object at 0x1574e8fb0>
  • Conventions :
    CF-1.0
    Info :
    Monthly ERA-Interim data. Downloaded and edited by fabien.maussion@uibk.ac.at
  • " ], "text/plain": [ "\n", @@ -1362,10 +1362,10 @@ " geometry GeometryIndex (crs=EPSG:4326)\n", "Attributes:\n", " Conventions: CF-1.0\n", - " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    • z
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      5
      units :
      m**2 s**-2
      long_name :
      Geopotential
      standard_name :
      geopotential
      [60 values with dtype=float64]
    • u
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      U component of wind
      standard_name :
      eastward_wind
      [60 values with dtype=float64]
    • v
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      V component of wind
      standard_name :
      northward_wind
      [60 values with dtype=float64]
    • level
      PandasIndex
      PandasIndex(Index([200, 500, 850], dtype='int32', name='level'))
    • month
      PandasIndex
      PandasIndex(Index([1, 7], dtype='int32', name='month'))
    • geometry
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x162620440>
  • Conventions :
    CF-1.0
    Info :
    Monthly ERA-Interim data. Downloaded and edited by fabien.maussion@uibk.ac.at
  • " ], "text/plain": [ "\n", @@ -1862,10 +1862,10 @@ " geometry GeometryIndex (crs=EPSG:4326)\n", "Attributes:\n", " Conventions: CF-1.0\n", - " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    • z
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      5
      units :
      m**2 s**-2
      long_name :
      Geopotential
      standard_name :
      geopotential
      [60 values with dtype=float64]
    • u
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      U component of wind
      standard_name :
      eastward_wind
      [60 values with dtype=float64]
    • v
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      V component of wind
      standard_name :
      northward_wind
      [60 values with dtype=float64]
    • level
      PandasIndex
      PandasIndex(Index([200, 500, 850], dtype='int32', name='level'))
    • month
      PandasIndex
      PandasIndex(Index([1, 7], dtype='int32', name='month'))
    • geometry
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x162621520>
  • Conventions :
    CF-1.0
    Info :
    Monthly ERA-Interim data. Downloaded and edited by fabien.maussion@uibk.ac.at
  • " ], "text/plain": [ "\n", @@ -2290,10 +2290,10 @@ " geometry GeometryIndex (crs=EPSG:4326)\n", "Attributes:\n", " Conventions: CF-1.0\n", - " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    • z
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      5
      units :
      m**2 s**-2
      long_name :
      Geopotential
      standard_name :
      geopotential
      [60 values with dtype=float64]
    • u
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      U component of wind
      standard_name :
      eastward_wind
      [60 values with dtype=float64]
    • v
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      V component of wind
      standard_name :
      northward_wind
      [60 values with dtype=float64]
    • level
      PandasIndex
      PandasIndex(Index([200, 500, 850], dtype='int32', name='level'))
    • month
      PandasIndex
      PandasIndex(Index([1, 7], dtype='int32', name='month'))
    • geometry
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x1626229c0>
  • Conventions :
    CF-1.0
    Info :
    Monthly ERA-Interim data. Downloaded and edited by fabien.maussion@uibk.ac.at
  • " ], "text/plain": [ "\n", @@ -2320,7 +2320,9 @@ } ], "source": [ - "extracted = ds.xvec.extract_points(gs, x_coords=\"longitude\", y_coords=\"latitude\", index=True)\n", + "extracted = ds.xvec.extract_points(\n", + " gs, x_coords=\"longitude\", y_coords=\"latitude\", index=True\n", + ")\n", "extracted" ] }, @@ -2719,13 +2721,13 @@ "Indexes:\n", " geometry GeometryIndex (crs=EPSG:4326)\n", "Attributes:\n", - " crs: epsg:4326
    • geometry
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x1626229c0>
  • crs :
    EPSG:4326
  • " ], "text/plain": [ "\n", @@ -2739,7 +2741,7 @@ "Indexes:\n", " geometry GeometryIndex (crs=EPSG:4326)\n", "Attributes:\n", - " crs: epsg:4326" + " crs: EPSG:4326" ] }, "execution_count": 11, @@ -3148,10 +3150,10 @@ " geometry GeometryIndex (crs=EPSG:4326)\n", "Attributes:\n", " Conventions: CF-1.0\n", - " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    • z
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      5
      units :
      m**2 s**-2
      long_name :
      Geopotential
      standard_name :
      geopotential
      [60 values with dtype=float64]
    • u
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      U component of wind
      standard_name :
      eastward_wind
      [60 values with dtype=float64]
    • v
      (month, level, geometry)
      float64
      ...
      number_of_significant_digits :
      2
      units :
      m s**-1
      long_name :
      V component of wind
      standard_name :
      northward_wind
      [60 values with dtype=float64]
    • level
      PandasIndex
      PandasIndex(Index([200, 500, 850], dtype='int32', name='level'))
    • month
      PandasIndex
      PandasIndex(Index([1, 7], dtype='int32', name='month'))
    • geometry
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x162634740>
  • Conventions :
    CF-1.0
    Info :
    Monthly ERA-Interim data. Downloaded and edited by fabien.maussion@uibk.ac.at
  • " ], "text/plain": [ "\n", @@ -3201,7 +3203,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/doc/source/geopandas.ipynb b/doc/source/geopandas.ipynb index 03f4604..8b0ab59 100644 --- a/doc/source/geopandas.ipynb +++ b/doc/source/geopandas.ipynb @@ -459,7 +459,7 @@ " * destination (destination) object POLYGON ((-87.60914087617012 41.8446925...\n", "Indexes:\n", " origin GeometryIndex (crs=GEOGCS["WGS84(DD)",DATUM["WGS_1984",SPHEROID["WGS84",6378137,29 ...)\n", - " destination GeometryIndex (crs=GEOGCS["WGS84(DD)",DATUM["WGS_1984",SPHEROID["WGS84",6378137,29 ...)
  • " ], "text/plain": [ " 1\u001b[0m \u001b[43mtraffic_counts\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2023-02-28\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtime\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m12\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mbike\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mxvec\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_geopandas\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Git/xvec/xvec/accessor.py:750\u001b[0m, in \u001b[0;36mXvecAccessor.to_geopandas\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 744\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 745\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot convert arrays with \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_obj\u001b[38;5;241m.\u001b[39mndim\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m dimensions into pandas \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 746\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mobjects. Requires 2 or fewer dimensions.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 747\u001b[0m )\n\u001b[1;32m 749\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_geom_indexes) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 750\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 751\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMultiple coordinates based on xvec.GeometryIndex are not supported \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 752\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas GeoPandas.GeoDataFrame cannot be indexed by geometry. Try using \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 753\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`.xvec.to_geodataframe()` instead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 754\u001b[0m )\n\u001b[1;32m 756\u001b[0m \u001b[38;5;66;03m# DataArray\u001b[39;00m\n\u001b[1;32m 757\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_obj, xr\u001b[38;5;241m.\u001b[39mDataArray):\n", + "File \u001b[0;32m~/Git/xvec/xvec/accessor.py:754\u001b[0m, in \u001b[0;36mXvecAccessor.to_geopandas\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 748\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 749\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot convert arrays with \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_obj\u001b[38;5;241m.\u001b[39mndim\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m dimensions into pandas \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 750\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mobjects. Requires 2 or fewer dimensions.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 751\u001b[0m )\n\u001b[1;32m 753\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_geom_indexes) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 754\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 755\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMultiple coordinates based on xvec.GeometryIndex are not supported \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 756\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas GeoPandas.GeoDataFrame cannot be indexed by geometry. Try using \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 757\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`.xvec.to_geodataframe()` instead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 758\u001b[0m )\n\u001b[1;32m 760\u001b[0m \u001b[38;5;66;03m# DataArray\u001b[39;00m\n\u001b[1;32m 761\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_obj, xr\u001b[38;5;241m.\u001b[39mDataArray):\n", "\u001b[0;31mValueError\u001b[0m: Multiple coordinates based on xvec.GeometryIndex are not supported as GeoPandas.GeoDataFrame cannot be indexed by geometry. Try using `.xvec.to_geodataframe()` instead." ] } @@ -1778,38 +1778,38 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x157efa7b0>
  • " ], "text/plain": [ "\n", @@ -1823,7 +1823,7 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 7, @@ -2400,7 +2400,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.12.0" }, "vscode": { "interpreter": { diff --git a/doc/source/index.md b/doc/source/index.md index e2df3e1..985bbe6 100644 --- a/doc/source/index.md +++ b/doc/source/index.md @@ -16,6 +16,7 @@ indexing projections geopandas extract_pts +zonal_stats io ``` diff --git a/doc/source/indexing.ipynb b/doc/source/indexing.ipynb index 109e6a0..a294ddb 100644 --- a/doc/source/indexing.ipynb +++ b/doc/source/indexing.ipynb @@ -416,7 +416,7 @@ "Coordinates:\n", " * payment_type (payment_type) <U11 'Credit card' 'Cash' ... 'Voided trip'\n", " * date (date) object 2008-12-31 2009-01-01 ... 2022-04-06 2022-05-18\n", - " * hour (hour) int64 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", + " * hour (hour) int32 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", " * origin (origin) object POLYGON ((933100.9183527103 192536.0856972...\n", " * destination (destination) object POLYGON ((933100.9183527103 192536.08...\n", "Data variables:\n", @@ -425,9 +425,9 @@ " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", - " origin GeometryIndex (crs=EPSG:2263)\n", - " destination GeometryIndex (crs=EPSG:2263)
  • " ], "text/plain": [ "\n", @@ -641,7 +641,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -1367,7 +1367,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -1866,7 +1866,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -2523,7 +2523,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -3152,7 +3152,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -3781,7 +3781,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -4411,7 +4411,7 @@ "Coordinates:\n", " * payment_type (payment_type)
  • " ], "text/plain": [ "\n", @@ -5044,7 +5044,7 @@ "Coordinates:\n", " * payment_type (payment_type)
    <xarray.DataArray 'PO60' (county: 3085)>\n",
            "array([ 4304,  3889, 17884, ..., 21583, 50164, 39260])\n",
            "Coordinates:\n",
    -       "  * county   (county) object POLYGON ((-95.34258270263672 48.54670333862305, ...
  • " ], "text/plain": [ "\n", @@ -899,20 +899,20 @@ " * county (county) object POLYGON ((-95.34258270263672 48.54670333862305, ...\n", " * year (year) int64 1960 1970 1980 1990\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x12e9bbb00>
  • " ], "text/plain": [ "\n", @@ -1339,38 +1339,38 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x14a815b50>
  • " ], "text/plain": [ "\n", @@ -1384,7 +1384,7 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 6, @@ -1491,7 +1491,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.12.0" }, "vscode": { "interpreter": { diff --git a/doc/source/io.ipynb b/doc/source/io.ipynb index b217a69..29a3a72 100644 --- a/doc/source/io.ipynb +++ b/doc/source/io.ipynb @@ -424,38 +424,38 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x153896780>
  • " ], "text/plain": [ "\n", @@ -469,7 +469,7 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 2, @@ -904,38 +904,38 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x157f08680>
  • " ], "text/plain": [ "\n", @@ -949,7 +949,7 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 4, @@ -1376,38 +1376,38 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x157f0aae0>
  • " ], "text/plain": [ "\n", @@ -1421,7 +1421,7 @@ " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 6, @@ -2038,59 +2038,55 @@ "Dimensions: (year: 4, county: 3085)\n", "Coordinates:\n", " * year (year) int64 1960 1970 1980 1990\n", - " * county (county) object POLYGON ((-95.34258270263672 48.54670333862...\n", + " * county (county) object MULTIPOLYGON (((-97.25125122070312 26.41930...\n", "Data variables:\n", - " population (year, county) int64 4304 3889 17884 ... 53427 250377 65077\n", - " unemployment (year, county) float64 7.9 8.2 10.1 7.5 ... 4.103 3.354 5.489\n", - " divorce (year, county) float64 1.859 2.863 2.711 ... 4.851 5.75 7.415\n", - " age (year, county) float64 28.8 25.9 29.6 ... 34.0 28.47 35.33\n", + " population (year, county) int64 151098 20084 180904 ... 83829 30167 62496\n", + " unemployment (year, county) float64 8.4 4.0 6.3 6.9 ... 4.464 6.393 4.379\n", + " divorce (year, county) float64 1.584 1.219 1.148 ... 9.628 7.917 8.998\n", + " age (year, county) float64 20.5 18.9 19.8 20.2 ... 37.0 27.2 34.1\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)
    • population
      (year, county)
      int64
      151098 20084 180904 ... 30167 62496
      array([[151098,  20084, 180904, ...,  30034,   9975,  41954],\n",
      +       "       [140368,  15570, 181535, ...,  31035,  11334,  50528],\n",
      +       "       [209727,  17495, 283229, ...,  51303,  13371,  54981],\n",
      +       "       [260120,  17705, 383545, ...,  83829,  30167,  62496]])
    • unemployment
      (year, county)
      float64
      8.4 4.0 6.3 ... 4.464 6.393 4.379
      array([[ 8.4       ,  4.        ,  6.3       , ...,  4.7       ,\n",
      +       "         6.5       ,  5.9       ],\n",
      +       "       [ 6.6       ,  7.3       ,  5.9       , ...,  3.6       ,\n",
      +       "         3.5       ,  3.        ],\n",
      +       "       [ 7.75660487, 11.33037329,  8.46864875, ...,  4.91240744,\n",
      +       "         6.96691176,  4.89821146],\n",
      +       "       [13.32703973, 15.10072274, 14.32961266, ...,  4.46391627,\n",
      +       "         6.39348523,  4.37875589]])
    • divorce
      (year, county)
      float64
      1.584 1.219 1.148 ... 7.917 8.998
      array([[1.58380698, 1.21930567, 1.14778151, ..., 2.16183453, 1.62022035,\n",
      +       "        2.02576951],\n",
      +       "       [1.90981927, 0.95937947, 1.31687243, ..., 3.09188981, 2.49451754,\n",
      +       "        2.50172931],\n",
      +       "       [3.20959126, 2.08742194, 2.53856077, ..., 6.21791514, 5.03355705,\n",
      +       "        6.65581939],\n",
      +       "       [4.39243717, 3.49016253, 3.57331943, ..., 9.62774751, 7.91690999,\n",
      +       "        8.99766695]])
    • age
      (year, county)
      float64
      20.5 18.9 19.8 ... 37.0 27.2 34.1
      array([[20.5, 18.9, 19.8, ..., 30.9, 21.8, 25.1],\n",
      +       "       [21.8, 20.9, 20.7, ..., 32.1, 23.6, 25.3],\n",
      +       "       [25. , 24.7, 24.1, ..., 33.4, 26.8, 30.1],\n",
      +       "       [27.4, 27.5, 26.1, ..., 37. , 27.2, 34.1]])
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='int64', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x1539f1b20>
  • " ], "text/plain": [ "\n", "Dimensions: (year: 4, county: 3085)\n", "Coordinates:\n", " * year (year) int64 1960 1970 1980 1990\n", - " * county (county) object POLYGON ((-95.34258270263672 48.54670333862...\n", + " * county (county) object MULTIPOLYGON (((-97.25125122070312 26.41930...\n", "Data variables:\n", - " population (year, county) int64 4304 3889 17884 ... 53427 250377 65077\n", - " unemployment (year, county) float64 7.9 8.2 10.1 7.5 ... 4.103 3.354 5.489\n", - " divorce (year, county) float64 1.859 2.863 2.711 ... 4.851 5.75 7.415\n", - " age (year, county) float64 28.8 25.9 29.6 ... 34.0 28.47 35.33\n", + " population (year, county) int64 151098 20084 180904 ... 83829 30167 62496\n", + " unemployment (year, county) float64 8.4 4.0 6.3 6.9 ... 4.464 6.393 4.379\n", + " divorce (year, county) float64 1.584 1.219 1.148 ... 9.628 7.917 8.998\n", + " age (year, county) float64 20.5 18.9 19.8 20.2 ... 37.0 27.2 34.1\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 8, @@ -2835,60 +2831,60 @@ "
    <xarray.Dataset>\n",
            "Dimensions:       (county: 3085, year: 4)\n",
            "Coordinates:\n",
    -       "  * county        (county) object POLYGON ((-95.34258270263672 48.54670333862...\n",
    +       "  * county        (county) object MULTIPOLYGON (((-97.25125122070312 26.41930...\n",
            "  * year          (year) object 1960 1970 1980 1990\n",
            "Data variables:\n",
    -       "    age           (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n",
    -       "    divorce       (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n",
    -       "    population    (county, year) int64 4304 3987 3764 4076 ... 43766 55800 65077\n",
    -       "    unemployment  (county, year) float64 7.9 9.0 5.903 ... 5.444 7.018 5.489\n",
    +       "    population    (county, year) int64 151098 140368 209727 ... 54981 62496\n",
    +       "    unemployment  (county, year) float64 8.4 6.6 7.757 13.33 ... 3.0 4.898 4.379\n",
    +       "    divorce       (county, year) float64 1.584 1.91 3.21 ... 2.502 6.656 8.998\n",
    +       "    age           (county, year) float64 20.5 21.8 25.0 27.4 ... 25.3 30.1 34.1\n",
            "Indexes:\n",
    -       "    county        GeometryIndex (crs=EPSG:4326)
    • year
      PandasIndex
      PandasIndex(Index([1960, 1970, 1980, 1990], dtype='object', name='year'))
    • county
      GeometryIndex (crs=EPSG:4326)
      <xvec.index.GeometryIndex object at 0x157f12510>
  • " ], "text/plain": [ "\n", "Dimensions: (county: 3085, year: 4)\n", "Coordinates:\n", - " * county (county) object POLYGON ((-95.34258270263672 48.54670333862...\n", + " * county (county) object MULTIPOLYGON (((-97.25125122070312 26.41930...\n", " * year (year) object 1960 1970 1980 1990\n", "Data variables:\n", - " age (county, year) float64 28.8 30.5 34.5 ... 26.4 28.97 35.33\n", - " divorce (county, year) float64 1.859 2.62 3.747 ... 2.725 4.782 7.415\n", - " population (county, year) int64 4304 3987 3764 4076 ... 43766 55800 65077\n", - " unemployment (county, year) float64 7.9 9.0 5.903 ... 5.444 7.018 5.489\n", + " population (county, year) int64 151098 140368 209727 ... 54981 62496\n", + " unemployment (county, year) float64 8.4 6.6 7.757 13.33 ... 3.0 4.898 4.379\n", + " divorce (county, year) float64 1.584 1.91 3.21 ... 2.502 6.656 8.998\n", + " age (county, year) float64 20.5 21.8 25.0 27.4 ... 25.3 30.1 34.1\n", "Indexes:\n", - " county GeometryIndex (crs=EPSG:4326)" + " county GeometryIndex (crs=EPSG:4326)" ] }, "execution_count": 10, @@ -2919,7 +2915,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.12.0" }, "vscode": { "interpreter": { diff --git a/doc/source/projections.ipynb b/doc/source/projections.ipynb index 9353dc3..7cdb970 100644 --- a/doc/source/projections.ipynb +++ b/doc/source/projections.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "tags": [ "hide-input" @@ -411,18 +411,18 @@ "Coordinates:\n", " * payment_type (payment_type) <U11 'Credit card' 'Cash' ... 'Voided trip'\n", " * date (date) object 2008-12-31 2009-01-01 ... 2022-04-06 2022-05-18\n", - " * hour (hour) int64 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", + " * hour (hour) int32 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", " * origin (origin) object POLYGON ((933100.9183527103 192536.0856972...\n", " * destination (destination) object POLYGON ((933100.9183527103 192536.08...\n", "Data variables:\n", - " trip_distance (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " trips_count (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " tip_amount (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " fare_amount (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", + " trip_distance (payment_type, date, hour, origin, destination) float64 na...\n", + " trips_count (payment_type, date, hour, origin, destination) float64 na...\n", + " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", + " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", " origin GeometryIndex (crs=EPSG:2263)\n", - " destination GeometryIndex (crs=EPSG:2263)
  • " ], "text/plain": [ "\n", @@ -476,20 +636,20 @@ "Coordinates:\n", " * payment_type (payment_type) \n", - " trips_count (payment_type, date, hour, origin, destination) float64 \n", - " tip_amount (payment_type, date, hour, origin, destination) float64 \n", - " fare_amount (payment_type, date, hour, origin, destination) float64 \n", + " trip_distance (payment_type, date, hour, origin, destination) float64 na...\n", + " trips_count (payment_type, date, hour, origin, destination) float64 na...\n", + " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", + " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", " origin GeometryIndex (crs=EPSG:2263)\n", " destination GeometryIndex (crs=EPSG:2263)" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -524,7 +684,6 @@ " \"fare_amount\": \"mean\",\n", " }\n", " ),\n", - " sparse=True,\n", ")\n", "\n", "# Replace int codes with labels\n", @@ -572,7 +731,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -586,7 +745,7 @@ " destination GeometryIndex (crs=EPSG:2263)" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -605,13 +764,13 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\n", + "\n", "Name: NAD83 / New York Long Island (ftUS)\n", "Axis Info [cartesian]:\n", "- X[east]: Easting (US survey foot)\n", @@ -620,14 +779,14 @@ "- name: United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk.\n", "- bounds: (-74.26, 40.47, -71.8, 41.3)\n", "Coordinate Operation:\n", - "- name: SPCS83 New York Long Island zone (US Survey feet)\n", + "- name: SPCS83 New York Long Island zone (US survey foot)\n", "- method: Lambert Conic Conformal (2SP)\n", "Datum: North American Datum 1983\n", "- Ellipsoid: GRS 1980\n", "- Prime Meridian: Greenwich" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -646,13 +805,13 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\n", + "\n", "Name: NAD83 / New York Long Island (ftUS)\n", "Axis Info [cartesian]:\n", "- X[east]: Easting (US survey foot)\n", @@ -661,14 +820,14 @@ "- name: United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk.\n", "- bounds: (-74.26, 40.47, -71.8, 41.3)\n", "Coordinate Operation:\n", - "- name: SPCS83 New York Long Island zone (US Survey feet)\n", + "- name: SPCS83 New York Long Island zone (US survey foot)\n", "- method: Lambert Conic Conformal (2SP)\n", "Datum: North American Datum 1983\n", "- Ellipsoid: GRS 1980\n", "- Prime Meridian: Greenwich" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -701,7 +860,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -1076,18 +1235,18 @@ "Coordinates:\n", " * payment_type (payment_type) <U11 'Credit card' 'Cash' ... 'Voided trip'\n", " * date (date) object 2008-12-31 2009-01-01 ... 2022-04-06 2022-05-18\n", - " * hour (hour) int64 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", + " * hour (hour) int32 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", " * origin (origin) object POLYGON ((933100.9183527103 192536.0856972...\n", " * destination (destination) object POLYGON ((933100.9183527103 192536.08...\n", "Data variables:\n", - " trip_distance (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " trips_count (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " tip_amount (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " fare_amount (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", + " trip_distance (payment_type, date, hour, origin, destination) float64 na...\n", + " trips_count (payment_type, date, hour, origin, destination) float64 na...\n", + " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", + " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", " origin GeometryIndex (crs=EPSG:4326)\n", - " destination GeometryIndex (crs=EPSG:3857)
  • " ], "text/plain": [ "\n", @@ -1141,20 +1460,20 @@ "Coordinates:\n", " * payment_type (payment_type) \n", - " trips_count (payment_type, date, hour, origin, destination) float64 \n", - " tip_amount (payment_type, date, hour, origin, destination) float64 \n", - " fare_amount (payment_type, date, hour, origin, destination) float64 \n", + " trip_distance (payment_type, date, hour, origin, destination) float64 na...\n", + " trips_count (payment_type, date, hour, origin, destination) float64 na...\n", + " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", + " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", " origin GeometryIndex (crs=EPSG:4326)\n", " destination GeometryIndex (crs=EPSG:3857)" ] }, - "execution_count": 8, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -1175,7 +1494,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -1550,18 +1869,18 @@ "Coordinates:\n", " * payment_type (payment_type) <U11 'Credit card' 'Cash' ... 'Voided trip'\n", " * date (date) object 2008-12-31 2009-01-01 ... 2022-04-06 2022-05-18\n", - " * hour (hour) int64 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", - " * origin (origin) object POLYGON ((-74.18445278891802 40.6949959774...\n", - " * destination (destination) object POLYGON ((-8258175.509239998 4967457....\n", + " * hour (hour) int32 0 1 2 3 4 5 6 7 8 ... 15 16 17 18 19 20 21 22 23\n", + " * origin (origin) object POLYGON ((-74.1844528021319 40.69499597816...\n", + " * destination (destination) object POLYGON ((-8258175.510710959 4967457....\n", "Data variables:\n", - " trip_distance (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " trips_count (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " tip_amount (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", - " fare_amount (payment_type, date, hour, origin, destination) float64 <COO: nnz=1268780, fill_value=nan>\n", + " trip_distance (payment_type, date, hour, origin, destination) float64 na...\n", + " trips_count (payment_type, date, hour, origin, destination) float64 na...\n", + " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", + " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", " origin GeometryIndex (crs=EPSG:4326)\n", - " destination GeometryIndex (crs=EPSG:3857)
  • " ], "text/plain": [ "\n", @@ -1615,20 +2094,20 @@ "Coordinates:\n", " * payment_type (payment_type) \n", - " trips_count (payment_type, date, hour, origin, destination) float64 \n", - " tip_amount (payment_type, date, hour, origin, destination) float64 \n", - " fare_amount (payment_type, date, hour, origin, destination) float64 \n", + " trip_distance (payment_type, date, hour, origin, destination) float64 na...\n", + " trips_count (payment_type, date, hour, origin, destination) float64 na...\n", + " tip_amount (payment_type, date, hour, origin, destination) float64 na...\n", + " fare_amount (payment_type, date, hour, origin, destination) float64 na...\n", "Indexes:\n", " origin GeometryIndex (crs=EPSG:4326)\n", " destination GeometryIndex (crs=EPSG:3857)" ] }, - "execution_count": 9, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -1677,7 +2156,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.12.0" }, "orig_nbformat": 4, "vscode": { diff --git a/doc/source/zonal_stats.ipynb b/doc/source/zonal_stats.ipynb new file mode 100644 index 0000000..6a2d9f2 --- /dev/null +++ b/doc/source/zonal_stats.ipynb @@ -0,0 +1,5230 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Zonal statistics\n", + "\n", + "A typical interaction between raster and vector data is zonal statistics - an aggregation of values of the raster that belong of a geographical region defined by a geometry. Vector data cubes are an ideal data structure for such a use case as they preserve the structure of the original cube and all its attributes while allowing you to index it by a polygon or linestring geometry. The geometry can represent any arbitrary geometry within the bounds of the original raster." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import geodatasets\n", + "import geopandas as gpd\n", + "import numpy as np\n", + "import rioxarray\n", + "import xarray as xr\n", + "\n", + "import xvec" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The example using the ERA-Interim reanalysis, monthly averages of upper level data:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:    (longitude: 480, latitude: 241, level: 3, month: 2)\n",
    +       "Coordinates:\n",
    +       "  * longitude  (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2\n",
    +       "  * latitude   (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0\n",
    +       "  * level      (level) int32 200 500 850\n",
    +       "  * month      (month) int32 1 7\n",
    +       "Data variables:\n",
    +       "    z          (month, level, latitude, longitude) float64 ...\n",
    +       "    u          (month, level, latitude, longitude) float64 ...\n",
    +       "    v          (month, level, latitude, longitude) float64 ...\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (longitude: 480, latitude: 241, level: 3, month: 2)\n", + "Coordinates:\n", + " * longitude (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2\n", + " * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + "Data variables:\n", + " z (month, level, latitude, longitude) float64 ...\n", + " u (month, level, latitude, longitude) float64 ...\n", + " v (month, level, latitude, longitude) float64 ...\n", + "Attributes:\n", + " Conventions: CF-1.0\n", + " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds = xr.tutorial.open_dataset(\"eraint_uvz\")\n", + "ds" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This Dataset is indexed by longitude and latitude representing the spatial grid. When aggregating using ``ds.xvec.zonal_stats``, you are replacing these two dimensions with a single one with shapely geometry.\n", + "\n", + "**Land mass geometry**\n", + "\n", + "Read the file representing the generalized global land mass as a set of polygons." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
    featureclascalerankmin_zoomgeometry
    0Land11.0POLYGON ((-59.57209 -80.04018, -59.86585 -80.5...
    1Land11.0POLYGON ((-159.20818 -79.49706, -161.12760 -79...
    2Land10.0POLYGON ((-45.15476 -78.04707, -43.92083 -78.4...
    3Land11.0POLYGON ((-121.21151 -73.50099, -119.91885 -73...
    4Land11.0POLYGON ((-125.55957 -73.48135, -124.03188 -73...
    ...............
    122Land11.0POLYGON ((51.13619 80.54728, 49.79368 80.41543...
    123Land00.0POLYGON ((99.93976 78.88094, 97.75794 78.75620...
    124Land00.0POLYGON ((-87.02000 79.66000, -85.81435 79.336...
    125Land00.0POLYGON ((-68.50000 83.10632, -65.82735 83.028...
    126Land00.0POLYGON ((-27.10046 83.51966, -20.84539 82.726...
    \n", + "

    127 rows × 4 columns

    \n", + "
    " + ], + "text/plain": [ + " featurecla scalerank min_zoom \\\n", + "0 Land 1 1.0 \n", + "1 Land 1 1.0 \n", + "2 Land 1 0.0 \n", + "3 Land 1 1.0 \n", + "4 Land 1 1.0 \n", + ".. ... ... ... \n", + "122 Land 1 1.0 \n", + "123 Land 0 0.0 \n", + "124 Land 0 0.0 \n", + "125 Land 0 0.0 \n", + "126 Land 0 0.0 \n", + "\n", + " geometry \n", + "0 POLYGON ((-59.57209 -80.04018, -59.86585 -80.5... \n", + "1 POLYGON ((-159.20818 -79.49706, -161.12760 -79... \n", + "2 POLYGON ((-45.15476 -78.04707, -43.92083 -78.4... \n", + "3 POLYGON ((-121.21151 -73.50099, -119.91885 -73... \n", + "4 POLYGON ((-125.55957 -73.48135, -124.03188 -73... \n", + ".. ... \n", + "122 POLYGON ((51.13619 80.54728, 49.79368 80.41543... \n", + "123 POLYGON ((99.93976 78.88094, 97.75794 78.75620... \n", + "124 POLYGON ((-87.02000 79.66000, -85.81435 79.336... \n", + "125 POLYGON ((-68.50000 83.10632, -65.82735 83.028... \n", + "126 POLYGON ((-27.10046 83.51966, -20.84539 82.726... \n", + "\n", + "[127 rows x 4 columns]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "world = gpd.read_file(geodatasets.get_path(\"naturalearth land\"))\n", + "world.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Default aggregation\n", + "\n", + "Using the `.xvec.zonal_stats` method with any array of geometries, like a ``geopandas.GeoSeries`` in this case, will create a `Dataset` (or a `DataArray` if the original object is a `DataArray`) indexed by the `GeometryIndex`:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:   (level: 3, month: 2, geometry: 127)\n",
    +       "Coordinates:\n",
    +       "  * level     (level) int32 200 500 850\n",
    +       "  * month     (month) int32 1 7\n",
    +       "  * geometry  (geometry) object POLYGON ((-59.57209469261153 -80.040178725096...\n",
    +       "Data variables:\n",
    +       "    z         (geometry, month, level) float64 1.1e+05 5.025e+04 ... 1.394e+04\n",
    +       "    u         (geometry, month, level) float64 2.401 1.482 ... 2.393 0.8898\n",
    +       "    v         (geometry, month, level) float64 0.4296 0.07286 ... 1.116 0.6399\n",
    +       "Indexes:\n",
    +       "    geometry  GeometryIndex (crs=EPSG:4326)\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (level: 3, month: 2, geometry: 127)\n", + "Coordinates:\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + " * geometry (geometry) object POLYGON ((-59.57209469261153 -80.040178725096...\n", + "Data variables:\n", + " z (geometry, month, level) float64 1.1e+05 5.025e+04 ... 1.394e+04\n", + " u (geometry, month, level) float64 2.401 1.482 ... 2.393 0.8898\n", + " v (geometry, month, level) float64 0.4296 0.07286 ... 1.116 0.6399\n", + "Indexes:\n", + " geometry GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " Conventions: CF-1.0\n", + " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aggregated = ds.xvec.zonal_stats(\n", + " world.geometry, x_coords=\"longitude\", y_coords=\"latitude\"\n", + ")\n", + "aggregated" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Aggregation options\n", + "\n", + "By default, the values are aggregated using `mean` but you have plenty of other options. For example, you may want to use `sum` instead." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:   (level: 3, month: 2, geometry: 127)\n",
    +       "Coordinates:\n",
    +       "  * level     (level) int32 200 500 850\n",
    +       "  * month     (month) int32 1 7\n",
    +       "  * geometry  (geometry) object POLYGON ((-59.57209469261153 -80.040178725096...\n",
    +       "Data variables:\n",
    +       "    z         (geometry, month, level) float64 3.301e+05 1.507e+05 ... 1.675e+07\n",
    +       "    u         (geometry, month, level) float64 7.203 4.446 ... 1.069e+03\n",
    +       "    v         (geometry, month, level) float64 1.289 0.2186 ... 1.341e+03 768.5\n",
    +       "Indexes:\n",
    +       "    geometry  GeometryIndex (crs=EPSG:4326)\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (level: 3, month: 2, geometry: 127)\n", + "Coordinates:\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + " * geometry (geometry) object POLYGON ((-59.57209469261153 -80.040178725096...\n", + "Data variables:\n", + " z (geometry, month, level) float64 3.301e+05 1.507e+05 ... 1.675e+07\n", + " u (geometry, month, level) float64 7.203 4.446 ... 1.069e+03\n", + " v (geometry, month, level) float64 1.289 0.2186 ... 1.341e+03 768.5\n", + "Indexes:\n", + " geometry GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " Conventions: CF-1.0\n", + " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aggregated_sum = ds.xvec.zonal_stats(\n", + " world.geometry, x_coords=\"longitude\", y_coords=\"latitude\", stats=\"sum\"\n", + ")\n", + "aggregated_sum" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Or pass a list of aggregations that will form another dimension of the resulting cube." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:           (level: 3, month: 2, zonal_statistics: 2, geometry: 127)\n",
    +       "Coordinates:\n",
    +       "  * level             (level) int32 200 500 850\n",
    +       "  * month             (month) int32 1 7\n",
    +       "  * zonal_statistics  (zonal_statistics) <U4 'mean' 'sum'\n",
    +       "  * geometry          (geometry) object POLYGON ((-59.57209469261153 -80.0401...\n",
    +       "Data variables:\n",
    +       "    z                 (geometry, zonal_statistics, month, level) float64 1.1e...\n",
    +       "    u                 (geometry, zonal_statistics, month, level) float64 2.40...\n",
    +       "    v                 (geometry, zonal_statistics, month, level) float64 0.42...\n",
    +       "Indexes:\n",
    +       "    geometry  GeometryIndex (crs=EPSG:4326)\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (level: 3, month: 2, zonal_statistics: 2, geometry: 127)\n", + "Coordinates:\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + " * zonal_statistics (zonal_statistics) \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:           (level: 3, month: 2, quantile: 3, zonal_statistics: 5,\n",
    +       "                       geometry: 127)\n",
    +       "Coordinates:\n",
    +       "  * level             (level) int32 200 500 850\n",
    +       "  * month             (month) int32 1 7\n",
    +       "  * quantile          (quantile) float64 0.1 0.2 0.3\n",
    +       "  * zonal_statistics  (zonal_statistics) <U9 'mean' 'sum' ... 'nanstd'\n",
    +       "  * geometry          (geometry) object POLYGON ((-59.57209469261153 -80.0401...\n",
    +       "Data variables:\n",
    +       "    z                 (geometry, zonal_statistics, month, level, quantile) float64 ...\n",
    +       "    u                 (geometry, zonal_statistics, month, level, quantile) float64 ...\n",
    +       "    v                 (geometry, zonal_statistics, month, level, quantile) float64 ...\n",
    +       "Indexes:\n",
    +       "    geometry  GeometryIndex (crs=EPSG:4326)\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (level: 3, month: 2, quantile: 3, zonal_statistics: 5,\n", + " geometry: 127)\n", + "Coordinates:\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + " * quantile (quantile) float64 0.1 0.2 0.3\n", + " * zonal_statistics (zonal_statistics) \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:         (level: 3, month: 2, world_polygons: 127)\n",
    +       "Coordinates:\n",
    +       "  * level           (level) int32 200 500 850\n",
    +       "  * month           (month) int32 1 7\n",
    +       "  * world_polygons  (world_polygons) object POLYGON ((-59.57209469261153 -80....\n",
    +       "    index           (world_polygons) int64 0 1 2 3 4 5 ... 122 123 124 125 126\n",
    +       "Data variables:\n",
    +       "    z               (world_polygons, month, level) float64 1.1e+05 ... 1.394e+04\n",
    +       "    u               (world_polygons, month, level) float64 2.401 ... 0.8898\n",
    +       "    v               (world_polygons, month, level) float64 0.4296 ... 0.6399\n",
    +       "Indexes:\n",
    +       "    world_polygons  GeometryIndex (crs=EPSG:4326)\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (level: 3, month: 2, world_polygons: 127)\n", + "Coordinates:\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + " * world_polygons (world_polygons) object POLYGON ((-59.57209469261153 -80....\n", + " index (world_polygons) int64 0 1 2 3 4 5 ... 122 123 124 125 126\n", + "Data variables:\n", + " z (world_polygons, month, level) float64 1.1e+05 ... 1.394e+04\n", + " u (world_polygons, month, level) float64 2.401 ... 0.8898\n", + " v (world_polygons, month, level) float64 0.4296 ... 0.6399\n", + "Indexes:\n", + " world_polygons GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " Conventions: CF-1.0\n", + " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aggregated = ds.xvec.zonal_stats(\n", + " world.geometry,\n", + " x_coords=\"longitude\",\n", + " y_coords=\"latitude\",\n", + " name=\"world_polygons\",\n", + " index=True,\n", + " all_touched=True,\n", + ")\n", + "aggregated" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Rasterization methods\n", + "\n", + "Xvec currently offers two methods used to rasterize the geometry. Both are implemented using `rasterio` but the default method (`rasterize`) creates a single categorical array based on input geometries using `rasterio.features.rasterize`. This is a very performant option but comes with a set of limitations. Each pixel can be allocated to a single geometry only, meaning that the aggregation for overlapping geometries will not be precise. Furhtermore, in situations when you have small polygons compared\n", + "to pixels, some polygons may not be represented in the categorical array and resulting statistics on them will be `nan`.\n", + "\n", + "Another option is to use `method=\"iterate\"`, which is using iteration over `rasterio.features.geometry_mask`. This method is significantly less performant even though it is by default executed in parallel (number of threads can be controlled by `n_jobs` where `-1` represents all available cores). On the other hand, it does not have the limitations of exclusivity as the `rasterize` method and can be more memory efficient." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.Dataset>\n",
    +       "Dimensions:   (geometry: 127, month: 2, level: 3)\n",
    +       "Coordinates:\n",
    +       "  * level     (level) int32 200 500 850\n",
    +       "  * month     (month) int32 1 7\n",
    +       "  * geometry  (geometry) object POLYGON ((-59.57209469261153 -80.040178725096...\n",
    +       "Data variables:\n",
    +       "    z         (geometry, month, level) float64 1.1e+05 5.025e+04 ... 1.394e+04\n",
    +       "    u         (geometry, month, level) float64 2.401 1.482 ... 2.393 0.8898\n",
    +       "    v         (geometry, month, level) float64 0.4296 0.07286 ... 1.116 0.6399\n",
    +       "Indexes:\n",
    +       "    geometry  GeometryIndex (crs=EPSG:4326)\n",
    +       "Attributes:\n",
    +       "    Conventions:  CF-1.0\n",
    +       "    Info:         Monthly ERA-Interim data. Downloaded and edited by fabien.m...
    " + ], + "text/plain": [ + "\n", + "Dimensions: (geometry: 127, month: 2, level: 3)\n", + "Coordinates:\n", + " * level (level) int32 200 500 850\n", + " * month (month) int32 1 7\n", + " * geometry (geometry) object POLYGON ((-59.57209469261153 -80.040178725096...\n", + "Data variables:\n", + " z (geometry, month, level) float64 1.1e+05 5.025e+04 ... 1.394e+04\n", + " u (geometry, month, level) float64 2.401 1.482 ... 2.393 0.8898\n", + " v (geometry, month, level) float64 0.4296 0.07286 ... 1.116 0.6399\n", + "Indexes:\n", + " geometry GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " Conventions: CF-1.0\n", + " Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m..." + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aggregated_iterative = ds.xvec.zonal_stats(\n", + " world.geometry,\n", + " x_coords=\"longitude\",\n", + " y_coords=\"latitude\",\n", + " method=\"iterate\",\n", + " n_jobs=-1,\n", + ")\n", + "aggregated_iterative" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Comparison with other methods\n", + "\n", + "A main difference compared to other methods available in the ecosystem is the resulting object being a vector data cube, preserving the original dimensionality of an Xarray object.\n", + "\n", + "### geocube\n", + "\n", + "[Geocube's method](https://corteva.github.io/geocube/stable/examples/zonal_statistics.html) for zonal statistics using `make_geocube` is in principle very similar to the implemenation using `method=\"rasterize`. Xvec's method is a bit more generic and does not require a rioxarray CRS attached to the object but requires a user to ensure that the data are using the same projection. The same zonal statistics from geocube documentation can be done using a single line of code.\n", + "\n", + "Load the data:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "ssurgo_data = gpd.read_file(\n", + " \"https://raw.githubusercontent.com/corteva/geocube/master/test/test_data/input/soil_data_group.geojson\"\n", + ")\n", + "ssurgo_data = ssurgo_data.loc[ssurgo_data.hzdept_r == 0]\n", + "\n", + "elevation = (\n", + " rioxarray.open_rasterio(\n", + " \"https://prd-tnm.s3.amazonaws.com/StagedProducts/Elevation/13/TIFF/current/n42w091/USGS_13_n42w091.tif\",\n", + " masked=True,\n", + " )\n", + " .rio.clip(ssurgo_data.geometry.values, ssurgo_data.crs, from_disk=True)\n", + " .sel(band=1)\n", + " .drop_vars(\"band\")\n", + ")\n", + "elevation.name = \"elevation\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate zonal statistics." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.DataArray 'elevation' (geometry: 7, zonal_statistics: 4)>\n",
    +       "array([[1.69547634e+02, 1.69539307e+02, 1.69804550e+02, 3.65312762e-02],\n",
    +       "       [1.73920127e+02, 1.69558762e+02, 1.89282532e+02, 4.23546672e+00],\n",
    +       "       [1.71910405e+02, 1.67691681e+02, 1.86318939e+02, 3.20585141e+00],\n",
    +       "       [1.76756619e+02, 1.70410980e+02, 1.80344055e+02, 2.74917394e+00],\n",
    +       "       [1.74971541e+02, 1.70244766e+02, 1.79659058e+02, 2.09304460e+00],\n",
    +       "       [1.76334406e+02, 1.69263535e+02, 1.94975769e+02, 3.93324959e+00],\n",
    +       "       [1.80006464e+02, 1.78314453e+02, 1.81538788e+02, 6.22535698e-01]])\n",
    +       "Coordinates:\n",
    +       "    spatial_ref       int64 0\n",
    +       "  * zonal_statistics  (zonal_statistics) <U4 'mean' 'min' 'max' 'std'\n",
    +       "  * geometry          (geometry) object MULTIPOLYGON (((-90.59735248065536 41...\n",
    +       "    mukey             (geometry) object '271425' '198692' ... '198750' '198724'\n",
    +       "Indexes:\n",
    +       "    geometry  GeometryIndex (crs=EPSG:4269)\n",
    +       "Attributes:\n",
    +       "    AREA_OR_POINT:          Area\n",
    +       "    BandDefinitionKeyword:  *\n",
    +       "    DataType:               *\n",
    +       "    LAYER_TYPE:             athematic\n",
    +       "    RepresentationType:     *\n",
    +       "    scale_factor:           1.0\n",
    +       "    add_offset:             0.0\n",
    +       "    long_name:              Layer_1
    " + ], + "text/plain": [ + "\n", + "array([[1.69547634e+02, 1.69539307e+02, 1.69804550e+02, 3.65312762e-02],\n", + " [1.73920127e+02, 1.69558762e+02, 1.89282532e+02, 4.23546672e+00],\n", + " [1.71910405e+02, 1.67691681e+02, 1.86318939e+02, 3.20585141e+00],\n", + " [1.76756619e+02, 1.70410980e+02, 1.80344055e+02, 2.74917394e+00],\n", + " [1.74971541e+02, 1.70244766e+02, 1.79659058e+02, 2.09304460e+00],\n", + " [1.76334406e+02, 1.69263535e+02, 1.94975769e+02, 3.93324959e+00],\n", + " [1.80006464e+02, 1.78314453e+02, 1.81538788e+02, 6.22535698e-01]])\n", + "Coordinates:\n", + " spatial_ref int64 0\n", + " * zonal_statistics (zonal_statistics) >> import geodatasets + >>> import geopandas as gpd + + A typical raster Dataset indexed by longitude and latitude: + + >>> ds = xr.tutorial.open_dataset("eraint_uvz") + >>> ds + + Dimensions: (longitude: 480, latitude: 241, level: 3, month: 2) + Coordinates: + * longitude (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2 + * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0 + * level (level) int32 200 500 850 + * month (month) int32 1 7 + Data variables: + z (month, level, latitude, longitude) float64 ... + u (month, level, latitude, longitude) float64 ... + v (month, level, latitude, longitude) float64 ... + Attributes: + Conventions: CF-1.0 + Info: Monthly ERA-Interim data. Downloaded + + A set of polygons representing land mass: + + >>> world = gpd.read_file(geodatasets.get_path("naturalearth land")) + >>> world + featurecla ... geometry + 0 Land ... POLYGON ((-59.57209 -80.04018, -59.86585 -80.5... + 1 Land ... POLYGON ((-159.20818 -79.49706, -161.12760 -79... + 2 Land ... POLYGON ((-45.15476 -78.04707, -43.92083 -78.4... + 3 Land ... POLYGON ((-121.21151 -73.50099, -119.91885 -73... + 4 Land ... POLYGON ((-125.55957 -73.48135, -124.03188 -73... + .. ... ... ... + 122 Land ... POLYGON ((51.13619 80.54728, 49.79368 80.41543... + 123 Land ... POLYGON ((99.93976 78.88094, 97.75794 78.75620... + 124 Land ... POLYGON ((-87.02000 79.66000, -85.81435 79.336... + 125 Land ... POLYGON ((-68.50000 83.10632, -65.82735 83.028... + 126 Land ... POLYGON ((-27.10046 83.51966, -20.84539 82.726... + + [127 rows x 4 columns] + + Dataset with N-1 dimensions indexed by the geometry aggregated using mean: + + >>> ds.xvec.zonal_stats(world.geometry, "longitude", "latitude") + + Dimensions: (level: 3, month: 2, geometry: 127) + Coordinates: + * level (level) int32 200 500 850 + * month (month) int32 1 7 + * geometry (geometry) object POLYGON ((-59.57209469261153 -80.040178725096... + Data variables: + z (geometry, month, level) float64 1.1e+05 5.025e+04 ... 1.394e+04 + u (geometry, month, level) float64 2.401 1.482 ... 2.393 0.8898 + v (geometry, month, level) float64 0.4296 0.07286 ... 1.116 0.6399 + Indexes: + geometry GeometryIndex (crs=EPSG:4326) + Attributes: + Conventions: CF-1.0 + Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m... + + Notes + ----- + + See the `User Guide <../zonal_stats.html>`_ for detailed explanation of the + functionality. + + See also + -------- + extract_points : extraction of values for the raster object for points """ if method == "rasterize": result = _zonal_stats_rasterize( @@ -1137,6 +1215,16 @@ def extract_points( Attributes: Conventions: CF-1.0 Info: Monthly ERA-Interim data. Downloaded and edited by fabien.m... + + Notes + ----- + + See the `User Guide <../extract_pts.html>`_ for detailed explanation of the + functionality. + + See also + -------- + zonal_stats : zonal statistics for polygons and linestrings """ if crs is None and hasattr(points, "crs"): crs = points.crs