forked from JavierLopatin/Python-Remote-Sensing-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtractValues.py
executable file
·188 lines (158 loc) · 5.37 KB
/
ExtractValues.py
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
174
175
176
177
178
179
180
181
182
183
184
185
#! /usr/bin/env python
#######################################################################################################
#
# ExtractValues.py
#
# A python script to extract raster values using a shapefile.
# Results are stored in a CSV file
#
# Author: Javier Lopatin
# Email: [email protected]
# Date: 09/08/2016
# Version: 1.0
#
# Info: Several statistics are allowed when applied to polygon shapefiles, including:
# - min
# - max
# - mean [default]
# - count
# - sum
# - std
# - median
# - majority
# - minority
# - unique
# - range
# - nodata
# - percentile
#
# Usage:
#
# python ExtractValues.py -r <Imput raster> -s <Imput shapefile>
# -i <Imput function> -s <Shapefile ID> -p <If shp are points>
#
# Examples for polygon shapefile: python ExtractValues.py -r raster.tif -s shape.shp -i ID
# python ExtractValues.py -r raster.tif -s shape.shp -f median -i ID
#
# Examples for points shapefile: python ExtractValues.py -r raster.tif -s shape.shp -i ID -p
#
########################################################################################################
import os, argparse, sys
import pandas as pd
import numpy as np
try:
import rasterio
except ImportError:
print("ERROR: Could not import Rasterio Python library.")
print("Check if Rasterio is installed.")
try:
from rasterstats import zonal_stats
except ImportError:
print("ERROR: Could not import rasterstats Python library.")
print("Check if rasterstats is installed.")
try:
import shapefile
except ImportError:
print("ERROR: Could not import PyShp Python library.")
print("Check if PyShp is installed.")
def ExtractValues(raster, shp, func, ID):
""" Extract raster values by a shapefile mask.
Several statistics are allowed.
"""
# Raster management
with rasterio.open(raster) as r:
bands = r.count
bandNames = []
for i in range(bands):
a = "B" + str(i+1)
bandNames.append(a)
# Shapefile management
shape = shapefile.Reader(shp)
records = pd.DataFrame(shape.records())
n = pd.DataFrame(shape.fields)[0].values.tolist().index(ID)
id = records[n-1]
# empty matrix to store results
matrix = np.empty((len(records), bands+1), dtype=object)
matrix[:,0] = id
colnamesSHP = [ID]
# Final colnames
colNames = colnamesSHP + bandNames
# Extract values
for i in range(bands):
# stats
stats = zonal_stats(shp, raster, stats=func, band=i+1)
x = pd.DataFrame(stats)
matrix[:,i+1] = x[func]
# set the final data frame
df = pd.DataFrame(matrix, columns=colNames)
return df
def ExtractPointValues(raster, shp, ID):
from rasterstats import point_query
""" Extract raster values by a shapefile point mask.
"""
# Raster management
with rasterio.open(raster) as r:
bands = r.count
bandNames = []
for i in range(bands):
a = "B" + str(i+1)
bandNames.append(a)
# Shapefile management
shape = shapefile.Reader(shp)
records = pd.DataFrame(shape.records())
n = pd.DataFrame(shape.fields)[0].values.tolist().index(ID)
id = records[n-1]
# empty matrix to store results
matrix = np.empty((len(records), bands+1), dtype=object)
matrix[:,0] = id
colnamesSHP = [ID]
# Final colnames
colNames = colnamesSHP + bandNames
# Extract values
for i in range(bands):
# stats
stats = point_query(shp, raster, band=i+1)
x = pd.DataFrame(stats)
matrix[:,i+1] = x[0]
# set the final data frame
df = pd.DataFrame(matrix, columns=colNames)
return df
if __name__ == "__main__":
# create the arguments for the algorithm
parser = argparse.ArgumentParser()
parser.add_argument('-r','--raster', help='Input raster', type=str)
parser.add_argument('-s', '--shapefile', help='Input shapefile', type=str)
parser.add_argument('-f', '--function', help='Input function to extract [default = "mean"]', type=str, default="mean")
parser.add_argument('-i', '--id', help='Shapefile ID to store in the CSV', type=str)
parser.add_argument('-p','--points', help='Shapefile are points', action="store_true", default=False)
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
args = vars(parser.parse_args())
# run Extraction
raster = args['raster']
shp = args['shapefile']
ID = args['id']
func = args['function']
# Check that the input parameter has been specified.
if args['raster'] == None:
# Print an error message if not and exit.
print("Error: No input image file provided.")
sys.exit()
if args['shapefile'] == None:
# Print an error message if not and exit.
print("Error: No input shapefile file provided.")
sys.exit()
if args['id'] == None:
# Print an error message if not and exit.
print("Error: No input id provided.")
sys.exit()
if args['points']==True:
if args['function'] == None:
# Print an error message if not and exit.
print("Error: No extracting function provided.")
sys.exit()
df = ExtractPointValues(raster, shp, ID)
else:
df = ExtractValues(raster, shp, func, ID)
# Save to CSV file
name = os.path.basename(raster)
df.to_csv(name[:-4] + ".csv", index=False, header=True, na_rep='NA')