In [1]:
%matplotlib inline
In [2]:
from pandarus import *
import geopandas as gpd
import pandas as pd
import rasterio
from rasterio import plot as rp
import os
import json
In [3]:
grid_fp = os.path.join('..', 'tests', 'data', 'grid.geojson')
points_fp = os.path.join('..', 'tests', 'data', 'points.geojson')
square_fp = os.path.join('..', 'tests', 'data', 'square.geojson')
lines_fp = os.path.join('..', 'tests', 'data', 'lines.geojson')
range_fp = os.path.join('..', 'tests', 'data', 'range.tif')
In [4]:
grid = gpd.read_file(grid_fp)
square = gpd.read_file(square_fp)
points = gpd.read_file(points_fp)
lines = gpd.read_file(lines_fp)
In [5]:
axes = grid.plot(color='white', figsize=(6, 6))
square.plot(ax=axes, alpha=0.5)
Out[5]:
In [6]:
spatial_result, json_data = intersect(
square_fp,
'name',
grid_fp,
'name',
compress=False
)
json.load(open(json_data))
Out[6]:
In [7]:
intersection = gpd.read_file(spatial_result)
intersection
Out[7]:
In [8]:
axes = grid.plot(color='white', figsize=(6, 6))
intersection.plot(ax=axes, alpha=0.5)
intersection.apply(
lambda x: axes.annotate(s=x['id'], xy=x.geometry.representative_point().coords[0], ha='center'),
axis=1
)
Out[8]:
In [9]:
axes = grid.plot(color='white', figsize=(6, 6))
lines.plot(linewidth=5, ax=axes)
Out[9]:
In [10]:
spatial_result, json_data = intersect(
lines_fp,
'name',
grid_fp,
'name',
compress=False
)
json.load(open(json_data))
Out[10]:
In [11]:
intersection = gpd.read_file(spatial_result)
intersection
Out[11]:
In [12]:
axes = grid.plot(color='white', figsize=(6, 6))
intersection.plot(linewidth=5, ax=axes)
intersection.apply(
lambda x: axes.annotate(
s=x['id'],
xy=x.geometry.representative_point().coords[0],
size=25),
axis=1
)
Out[12]:
In [13]:
intersection
Out[13]:
In [14]:
axes = grid.plot(color='white', figsize=(6, 6))
points.plot(markersize=10, ax=axes)
Out[14]:
In [15]:
spatial_result, json_data = intersect(
points_fp,
'name',
grid_fp,
'name',
compress=False
)
json.load(open(json_data))
Out[15]:
In [16]:
intersection = gpd.read_file(spatial_result)
intersection
Out[16]:
In [17]:
ax = grid.plot(color='white', figsize=(6, 6))
grid.apply(lambda x: ax.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=ax)
In [18]:
json_data = raster_statistics(
grid_fp,
'name',
range_fp,
compress=False
)
rs_data = json.load(open(json_data))
rs_data
Out[18]:
In [19]:
merged = grid.merge(pd.DataFrame([(x, y['mean']) for x, y in rs_data['data']], columns=['name', 'value']))
ax = merged.plot(column='value', figsize=(6, 6))
merged.apply(lambda x: ax.annotate(
s="{:2.1f}".format(x['value']),
xy=x.geometry.representative_point().coords[0],
ha='center',
size=20
), axis=1)
Out[19]:
In [20]:
ax = lines.plot(color="black", figsize=(6, 6))
lines.apply(lambda x: ax.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=ax, alpha=0.5)
In [21]:
json_data = raster_statistics(
lines_fp,
'name',
range_fp,
compress=False
)
rs_data = json.load(open(json_data))
rs_data
Out[21]:
In [22]:
merged = lines.merge(pd.DataFrame([(x, y['mean']) for x, y in rs_data['data']], columns=['name', 'value']))
ax = merged.plot(column='value', figsize=(6, 6))
merged.apply(lambda x: ax.annotate(
s="{:2.1f}".format(x['value']),
xy=x.geometry.representative_point().coords[0],
va="center",
size=20
), axis=1)
Out[22]:
In [23]:
ax = points.plot(color="black", figsize=(6, 6), markersize=6)
points.apply(lambda x: ax.annotate(
s=x['name'],
xy=x.geometry.representative_point().coords[0],
va='top',
size=16
), axis=1)
with rasterio.open(range_fp) as raster:
img = rp.show(raster, ax=ax, alpha=0.5)
In [24]:
json_data = raster_statistics(
points_fp,
'name',
range_fp,
compress=False
)
rs_data = json.load(open(json_data))
rs_data
Out[24]:
In [25]:
merged = points.merge(pd.DataFrame([(x, y['mean']) for x, y in rs_data['data']], columns=['name', 'value']))
ax = merged.plot(column='value', figsize=(6, 6), color='black', markersize=6)
merged.apply(lambda x: ax.annotate(
s="{:2.1f}".format(x['value']),
xy=x.geometry.representative_point().coords[0],
va="top",
size=20
), axis=1)
with rasterio.open(range_fp) as raster:
img = rp.show(raster, ax=ax, alpha=0.5)
In [ ]: