-
Notifications
You must be signed in to change notification settings - Fork 902
/
geopandas_rasterio_sample.ipynb.txt
173 lines (173 loc) · 3.95 KB
/
geopandas_rasterio_sample.ipynb.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Using GeoPandas with Rasterio to sample point data\n",
"\n",
"This example shows how to use GeoPandas with Rasterio. [Rasterio](https://rasterio.readthedocs.io/en/latest/index.html) is a package for reading and writing raster data.\n",
"\n",
"In this example a set of vector points is used to sample raster data at those points.\n",
"\n",
"The raster data used is Copernicus Sentinel data 2018 for Sentinel data.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import geopandas\n",
"import rasterio\n",
"import matplotlib.pyplot as plt\n",
"from shapely.geometry import Point"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create example vector data\n",
"=============================\n",
"\n",
"Generate a geodataframe from a set of points\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create sampling points\n",
"points = [Point(625466, 5621289), Point(626082, 5621627), Point(627116, 5621680), Point(625095, 5622358)]\n",
"gdf = geopandas.GeoDataFrame([1, 2, 3, 4], geometry=points, crs=32630)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``GeoDataFrame`` looks like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdf.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Open the raster data\n",
"=============================\n",
"\n",
"Use ``rasterio`` to open the raster data to be sampled"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"src = rasterio.open('s2a_l2a_fishbourne.tif')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see the raster data with the point data overlaid.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"nbsphinx-thumbnail"
]
},
"outputs": [],
"source": [
"from rasterio.plot import show\n",
"\n",
"fig, ax = plt.subplots()\n",
"\n",
"# transform rasterio plot to real world coords\n",
"extent=[src.bounds[0], src.bounds[2], src.bounds[1], src.bounds[3]]\n",
"ax = rasterio.plot.show(src, extent=extent, ax=ax, cmap='pink')\n",
"\n",
"gdf.plot(ax=ax)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sampling the data\n",
"===============\n",
"Rasterio requires a list of the coordinates in x,y format rather than as the points that are in the geomentry column.\n",
"\n",
"This can be achieved using the code below"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"coord_list = [(x,y) for x,y in zip(gdf['geometry'].x , gdf['geometry'].y)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Carry out the sampling of the data and store the results in a new column called `value`. Note that if the image has more than one band, a value is returned for each band."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdf['value'] = [x for x in src.sample(coord_list)]\n",
"gdf.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}