Skip to main content

xshape

https://img.shields.io/pypi/v/xshape.svg https://img.shields.io/travis/ClimateImpactLab/xshape.svg Documentation Status Updates

Tools for working with shapefiles, topographies, and polygons in xarray

Features

  • Read a shapefile and obtain an xarray DataArray of field records

  • Draw shapefile boundaries on gridded data

  • Plot xarray DataArray data indexed by shapefile records as a choropleth

Usage

Getting records for fields in a shapefile

In [1]: import xshape

In [2]: fields, polygons = xshape.parse_shapefile(
   ...:     'tests/data/shapefiles/CA_counties/CA_counties',
   ...:     encoding='latin1')

In [3]: fields
Out[3]:
<xarray.Dataset>
Dimensions:   (shape: 58)
Coordinates:
  * shape     (shape) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
Data variables:
    STATEFP   (shape) <U22 '06' '06' '06' '06' '06' '06' '06' '06' '06' '06' ...
    COUNTYFP  (shape) <U22 '107' '009' '047' '079' '097' '041' '023' '051' ...
    COUNTYNS  (shape) <U22 '00277318' '01675885' '00277288' '00277304' ...
    GEOID     (shape) <U22 '06107' '06009' '06047' '06079' '06097' '06041' ...
    NAME      (shape) <U22 'Tulare' 'Calaveras' 'Merced' 'San Luis Obispo' ...
    NAMELSAD  (shape) <U22 'Tulare County' 'Calaveras County' ...
    LSAD      (shape) <U22 '06' '06' '06' '06' '06' '06' '06' '06' '06' '06' ...
    CLASSFP   (shape) <U22 'H1' 'H1' 'H1' 'H1' 'H1' 'H1' 'H1' 'H1' 'H1' 'H1' ...
    MTFCC     (shape) <U22 'G4020' 'G4020' 'G4020' 'G4020' 'G4020' 'G4020' ...
    CSAFP     (shape) <U22 '' '' '' '' '488' '488' '' '' '' '' '488' '472' ...
    CBSAFP    (shape) <U22 '47300' '' '32900' '42020' '42220' '41860' ...
    METDIVFP  (shape) <U22 '' '' '' '' '' '41884' '' '' '' '' '36084' '' '' ...
    FUNCSTAT  (shape) <U22 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' ...
    ALAND     (shape) <U22 '12494707314' '2641820029' '5011554680' ...
    AWATER    (shape) <U22 '37391604' '43810423' '112760479' '820974619' ...
    INTPTLAT  (shape) <U22 '+36.2288317' '+38.1846184' '+37.1948063' ...
    INTPTLON  (shape) <U22 '-118.7810618' '-120.5593996' '-120.7228019' ...

Drawing shape boundaries on gridded data

In [4]: import xarray as xr, numpy as np, xshape

# generate sample data
In [5]: da = xr.DataArray(
   ...:    np.cos((
   ...:         np.arange(41*45).reshape((41, 45)) * np.arange(41*45).reshape((45, 41)
   ...:         ).T)/4e5),
   ...:    dims=('lat', 'lon'),
   ...:    coords={
   ...:        'lon': np.linspace(-125, -114, 45),
   ...:        'lat': np.linspace(32, 42, 41)})
   ...:

In [6]: da.xshape.overlay(
   ...:     'tests/data/shapefiles/CA_counties/CA_counties',
   ...:     encoding='latin1',
   ...:     cmap='YlGnBu');
   ...:
docs/images/california_map.png

Plotting regional data in a choropleth

Using the xarray extension, we can plot DataArray data directly:

In [7]: import xshape, xarray as xr, pandas as pd

In [8]: df = pd.read_csv('tests/data/datasets/co-est2016.csv', encoding='latin1')
   ...: ca = df[(df['STATE'] == 6) & (df['COUNTY'] > 0)].copy()
   ...: ca['fips'] = df['STATE'] * 1000 + df['COUNTY']
   ...: da = ca.set_index(['fips'])['POPESTIMATE2016'].to_xarray()
   ...: da.coords['GEOID'] = ('fips', ), list(map('{:05}'.format, da.fips.values))
   ...: da = da.swap_dims({'fips': 'GEOID'})

In [9]: da.xshape.plot(
   ...:     'tests/data/shapefiles/CA_counties/CA_counties',
   ...:     encoding='latin1',
   ...:     cmap='YlGnBu');
   ...:
docs/images/california_map_pop.png

We can also combine the information from the fields with the data contained in the DataArray:

In [10]: land_area = (
   ....:     fields
   ....:     .set_coords('GEOID')
   ....:     .swap_dims({'shape': 'GEOID'})
   ....:     .ALAND.astype(float))

In [11]: np.log(da / land_area).xshape.plot(
   ....:     'tests/data/shapefiles/CA_counties/CA_counties',
   ....:     encoding='latin1',
   ....:     cmap='YlGnBu');
   ....:
docs/images/california_map_pop_per_m2.png

TODO

  • Use shapefiles to reshape gridded/pixel data

History

0.1.0 (2018-01-13)

  • First release on PyPI.

Metadata

Release files for xshape 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xshape 0.1.1
File Size Uploaded
xshape-0.1.1.tar.gz 3.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for xshape 0.1.1
File Interpreter ABI Platform
xshape-0.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 3.0 MB

Release files / xshape-0.1.1.tar.gz

Download URL xshape-0.1.1.tar.gz
Size 3.0 MB
Tags Source
SHA-256 checksum
How to use checksums
a7eb34e55fe8aeb31ece1dd6a679b2bdb4ebe1dd0427e3138220373ea5f56c18
BLAKE2b-256 checksum
How to use checksums
32bc5508489663182c16ebbc7ddec6f20e478345a67f8e017fd73938058d77ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / xshape-0.1.1-py2.py3-none-any.whl

Download URL xshape-0.1.1-py2.py3-none-any.whl
Size 9.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0139de2b25e1b74dfab9867776e355b02a24155c511ca53554703b1787192cf2
BLAKE2b-256 checksum
How to use checksums
97d800e17177ba152d832c7a043a498bd6e12a1555b7b843271c98d888fd5770
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page