In [1]:
%matplotlib inline
In [2]:
from matplotlib import pyplot as plt
from pandarus import *
from rasterio import plot as rp
import geopandas as gpd
import json
import os
import pandas as pd
import rasterio
In [3]:
ls ../tests/data/
In [4]:
provinces = gpd.read_file(os.path.join("..", "tests", "data", "test_provinces.gpkg"))
big_grid = gpd.read_file(os.path.join("..", "tests", "data", "big-grid.geojson"))
grid_fp = os.path.join("..", "tests", "data", "grid.geojson")
grid = gpd.read_file(grid_fp)
range_fp = os.path.join('..', 'tests', 'data', 'range.tif')
grid_fp = os.path.join('..', 'tests', 'data', 'grid.geojson')
outside_fp = os.path.join('..', 'tests', 'data', 'outside.geojson')
In [32]:
figure, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(16, 8))
provinces.plot(color='white', figsize=(6, 6), ax=ax1, linewidth=2)
big_grid.plot(ax=ax1, alpha=0.5, facecolor='None', edgecolor='blue', linewidth=2)
ax1.set_xlim(-0.2, 2.2)
ax1.set_ylim(6.8, 9.2)
ax1.set_xticks([])
ax1.set_yticks([])
spatial_result, _ = intersect(
"../tests/data/big-grid.geojson",
'name',
"../tests/data/test_provinces.gpkg",
'name'
)
intersection = gpd.read_file(spatial_result)
intersection.plot(ax=ax2, alpha=0.5)
intersection.apply(
lambda x: ax2.annotate(
s="{:2.2g}".format(x['measure'] / 1e9),
xy=x.geometry.representative_point().coords[0],
ha='center',
va='center'),
axis=1
)
ax2.axis('off')
plt.savefig("images/two-vectors.png", bbox_inches='tight', transparent=True)
In [6]:
intersections_fp, _ = intersect(outside_fp, "name", grid_fp, "name")
remaining_fp = calculate_remaining(outside_fp, 'name', intersections_fp, compress=False)
remaining_data = json.load(open(remaining_fp))
print(remaining_data)
ax = grid.plot(color='white', figsize=(6, 6), linewidth=2)
# gpd.read_file(outside_fp).plot(axes=ax, alpha=0.5, facecolor='None', edgecolor='blue', linewidth=2)
def raise_up(coords):
print(coords)
return (coords[0], coords[1] + 0.25)
outside = gpd.read_file(outside_fp)
merged = outside.merge(pd.DataFrame(remaining_data['data'], columns=['name', 'outside']))
merged.plot(axes=ax, alpha=0.5, facecolor='None', edgecolor='blue', linewidth=2)
merged.apply(
lambda x: ax.annotate(
s="{:2.2g}".format(x['outside'] / 1e9),
xy=raise_up(x.geometry.representative_point().coords[0]),
ha='center',
va='center',
size=15),
axis=1
)
ax.text(
0.75,
1.75,
'3.1',
size=15,
va='center',
ha='center'
)
ax.text(
1.25,
1.75,
'3.1',
size=15,
va='center',
ha='center'
)
ax.axis('off')
plt.savefig("images/outside.png", bbox_inches='tight', transparent=True)
In [ ]:
In [ ]:
In [15]:
json_data = json.load(open(Pandarus(
grid_fp,
from_metadata={'field': 'name'},
).rasterstats(range_fp, compressed=False)))
In [23]:
merged = grid.merge(pd.DataFrame([(x, y['mean']) for x, y in json_data['data']], columns=['name', 'value']))
In [26]:
figure, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(16, 8))
grid.plot(color='white', ax=ax1)
grid.apply(lambda x: ax1.annotate(
s=x['name'],
xy=x.geometry.representative_point().coords[0],
ha='center',
size=16
), axis=1)
with rasterio.open(range_fp) as raster:
img = rp.show(raster, ax=ax1)
ax1.axis('off')
ax = merged.plot(column='value', figsize=(6, 6), ax=ax2)
merged.apply(lambda x: ax2.annotate(
s="{:2.1f}".format(x['value']),
xy=x.geometry.representative_point().coords[0],
ha='center',
size=20
), axis=1)
ax2.axis('off')
plt.savefig("images/rasterstats.png", bbox_inches='tight', transparent=True)
pass