-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathf_geovalidate.py
316 lines (254 loc) · 10.5 KB
/
f_geovalidate.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# if overlaps and unique, print (also unique)
from frictionless import Resource
import geopandas as gpd
import pandas as pd
import json
import pathlib
import os
import tempfile
import math
import sys
# Schema
def readSchema(filePath):
f = open(filePath)
schema = json.load(f)
return(schema)
def getGeoFields(schema, index = 0):
geoFields = [elt for elt in schema['fields'] if elt['type'] == 'wkt']
return(geoFields)
def getGeoField(schema, index = 0):
geoFields = getGeoFields(schema, index)
geoField = geoFields[index]
return(geoField)
# Bounds
def controlXmin(xmin, refXmin) :
ok = (xmin >= refXmin)
return(ok)
def controlYmin(ymin, refYmin) :
ok = (ymin >= refYmin)
return(ok)
def controlXmax(xmax, refXmax) :
ok = (xmax <= refXmax)
return(ok)
def controlYmax(ymax, refYmax) :
ok = (ymax <= refYmax)
return(ok)
# Unique
def controlUnique(geom1, geom2):
ok = (geom1 != geom2)
return(ok)
# Overlaps
def controlOverlaps(geom1, geom2):
ok = (geom1.overlaps(geom2) is False)
return(ok)
# Geometry types
def getMultiGeomType(refGeomType):
if refGeomType in ['point', 'multipoint']:
refGeomTypes = ['point', 'multipoint']
if refGeomType in ['linestring', 'multilinestring']:
refGeomTypes = ['linestring', 'multilinestring']
if refGeomType in ['polygon', 'multipolygon']:
refGeomTypes = ['polygon', 'multipolygon']
return(refGeomTypes)
def getMultiGeomTypes(refGeomTypes):
res = [getMultiGeomType(elt) for elt in refGeomTypes]
res = [item for sublist in res for item in sublist]
return(res)
def controlGeomType(geomType, refGeomType):
geomType = geomType.lower()
if isinstance(refGeomType, str):
refGeomType = refGeomType.lower()
refGeomTypes = getMultiGeomType(refGeomType)
ok = (geomType in refGeomTypes)
elif isinstance(refGeomType, list):
refGeomTypes = [elt.lower() for elt in refGeomType]
refGeomTypes = getMultiGeomTypes(refGeomTypes)
ok = (geomType in refGeomTypes)
return(ok)
# CRS
def controlCRS(crs, refCrs):
ok = crs.lower() == refCrs.lower()
if not ok:
print('%s CRS is required but %s is found'%(refCrs, crs))
return(ok)
# Valid
def controlValid(geom):
ok = geom.is_valid
return(ok)
# Area
def controlArea(geom, crs, minArea):
ok = (gpd.GeoSeries(geom).set_crs(crs).to_crs('EPSG:3857').area > minArea).all()
return(ok)
# Bounds
def controlBounds(geom, geomBounds):
ok = (geomBounds.contains(geom)).all()
return(ok)
# Do geometries exist for all lines ?
def getEmptyGeomRows(data, geomCol):
ok = True
n = data[data[geomCol].isnull()].shape[0]
emptyGeomRows = list()
if n > 0:
ok = False
for i in range(data.shape[0]):
value = data[geomCol][i]
if isinstance(value, float):
if math.isnan(value):
emptyGeomRows.append(i)
print('[Warning] %d rows without geometries were found : %s.\n'%(len(emptyGeomRows), ', '.join([str(elt) for elt in emptyGeomRows])))
return(emptyGeomRows)
# Does the geometry column exist ?
def controlGeomColExists(data, geomCol) :
ok = geomCol in data.columns
return(ok)
# Maps initial data frame with filtered data frame
def getMapping(nRows, emptyGeomRows):
d = dict()
j = 0
for i in range(nRows):
if i in emptyGeomRows:
d[i] = None
else:
d[i] = j
j += 1
return(d)
# Geom from WKT
def getGeomFromWkt(wkt):
geom = gpd.GeoSeries.from_wkt([wkt])
return(geom)
def readData(filePath, schema, geomCol = None):
print('File : %s\n'%filePath)
fileExtension = pathlib.Path(filePath).suffix
if fileExtension == '.csv' :
# Filter data
data = pd.read_csv(filePath)
nRows = data.shape[0]
# Get geometry column from schema
if geomCol is None:
geoFields = getGeoFields(schema)
geomCol = getGeoField(schema)['name']
if len(geoFields) == 1:
print("Geometry column : '%s' is the geometry column according to the schema\n"%geomCol)
else :
print("%d geometry columns are present in the schema."%(len(geoFields)))
print("Only %s will be analysed.\n"%(geomCol))
print("You can choose the geometry column that will be controlled with :")
print("> geovalidate data.csv schema.json wktcolumn")
print("")
# Check if geometry column exists
if not controlGeomColExists(data, geomCol):
sys.exit("[Error] '%s' geometry column does not exist. Exiting..."%geomCol)
else:
print("Analyzing '%s' column...\n"%geomCol)
# Get rows without geometries
emptyGeomRows = getEmptyGeomRows(data, geomCol)
if len(emptyGeomRows) > 0 :
# Filter non null geometries
data = data[data[geomCol].notnull()]
# Write CSV
tempDir = tempfile.gettempdir()
filePath = pathlib.Path(tempDir)/(pathlib.Path(filePath).stem+'.csv')
data.to_csv(filePath)
# Get mapping dictionary
mapping = getMapping(nRows, emptyGeomRows)
# Rename CSV
if geomCol != '_geom':
# Rename
data = pd.read_csv(filePath).rename(columns = {geomCol:'_geom'})
# Write CSV
tempDir = tempfile.gettempdir()
filePath = pathlib.Path(tempDir)/(pathlib.Path(filePath).stem+'.csv')
data.to_csv(filePath)
# Read CSV
data = Resource(filePath)
# Write GeoJSON
tempDir = tempfile.gettempdir()
tempGeoJSON = pathlib.Path(tempDir)/(pathlib.Path(filePath).stem+'.geojson')
data.write(tempGeoJSON)
# Read Data
data = gpd.read_file(tempGeoJSON)
else:
# Read Data
data = gpd.read_file(filePath)
res = {'data' : data, 'mapping' : mapping}
return(res)
# Process
def geovalidate(dataPath, schemaPath, geomCol = None):
# Schema
if not os.path.exists(schemaPath):
sys.exit('[Error] %s does not exist. Exiting...'%schemaPath)
schema = readSchema(schemaPath)
geoField = getGeoField(schema)
# Data
if not os.path.exists(dataPath):
sys.exit('[Error] %s does not exist. Exiting...'%dataPath)
# Read Data
res = readData(dataPath, schema, geomCol)
data = res['data']
mapping = res['mapping']
# Control
for elt in mapping:
# NULL GEOMETRIES
if mapping[elt] is None:
print('%d : no geometry'%elt)
else:
i = mapping[elt]
geom = data.geometry[i]
# ~ # CRS
# ~ if 'crs' in geoField.keys():
# ~ okCRS = controlCRS(data.crs.srs, geoField['crs'])
# ~ if not okCRS:
# ~ print('%d : wrong %s CRS. Must be %s'%(i, data.crs.srs, geoField['crs']))
# GEOMTYPE
if 'geomtype' in geoField.keys():
okGeomType = controlGeomType(geom.geom_type, geoField['geomtype'])
if not okGeomType:
print("%d : '%s' is found but '%s' geometry type is required"%(i, geom.geom_type, geoField['geomtype']))
# VALID
okValid = controlValid(geom)
if not okValid:
print('%d : geometry not valid'%i)
if 'constraints' in geoField.keys():
# BOUNDS
if 'bounds' in geoField['constraints'].keys():
bb = geom.bounds
refbb = geoField['constraints']['bounds']
if isinstance(refbb, list):
okXmin = controlXmin(bb[0], refbb[0])
if not okXmin:
print('%d : xmin too low. xmin (%f) is inferior to xmin bounds (%f)'%(i, bb[0], refbb[0]))
okYmin = controlYmin(bb[1], refbb[1])
if not okYmin:
print('%d : ymin too low. ymin (%f) is inferior to ymin bounds (%f)'%(i, bb[1], refbb[1]))
okXmax = controlXmax(bb[2], refbb[2])
if not okXmax:
print('%d : xmax too high. xmax (%f) is superior to xmax bounds (%f)'%(i, bb[2], refbb[2]))
okYmax = controlYmax(bb[3], refbb[3])
if not okYmax:
print('%d : ymax too high. ymax (%f) is superior to ymax bounds (%f)'%(i, bb[3], refbb[3]))
elif isinstance(refbb, str):
geomBounds = getGeomFromWkt(refbb)
okBounds = controlBounds(geom, geomBounds)
if not okBounds:
print('%d : geometry is not contained by the bounds geometry'%i)
# MINAREA
if 'minArea' in geoField['constraints'].keys():
okArea = controlArea(geom, data.crs.srs, geoField['constraints']['minArea'])
if not okArea:
print('%d : area too small. It is smaller than %d square meters'%(i, geoField['constraints']['minArea']))
# UNIQUE
if 'unique' in geoField['constraints'].keys():
if geoField['constraints']['unique'] :
for j in range(data.geometry.count()):
if j != i:
okUnique = controlUnique(geom, data.geometry[j])
if not okUnique:
print('%d : equals %d'%(i, j))
# OVERLAPS
if 'overlaps' in geoField['constraints'].keys():
if not geoField['constraints']['overlaps'] :
for j in range(data.geometry.count()):
if j != i:
okOverlaps = controlOverlaps(geom, data.geometry[j])
if not okOverlaps:
print('%d : overlaps %d'%(i, j))