forked from lmotta/scripts-for-gis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
union_neighbour_origin.py
402 lines (349 loc) · 13.1 KB
/
union_neighbour_origin.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : Union neighbour
Description : Union neighbour polygon
Arguments : Shapefiles
-------------------
begin : 2015-12-14
copyright : (C) 2015 by Luiz Motta
email : motta dot luiz at gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
#ogr2ogr -overwrite example01_neighbour.shp example01_neighbour.geojson
import os, sys
import argparse
import datetime
try:
from osgeo import ogr
from osgeo import osr
except ImportError:
import ogr
import osr
def printStatus(status, newLine=False):
if newLine:
ch = "\n"
else:
ch = ""
sys.stdout.write( "\r%s" % ( status.ljust(100) + ch ) )
sys.stdout.flush()
def run(name_file, year):
def getLayer(name_file):
inDataSource = driver.Open( name_file, 0 )
#
if inDataSource is None:
printStatus( "File '%s' can't open" % name_file, True )
return None
#
inLayer = inDataSource.GetLayer()
#
return ( inDataSource, inLayer)
def getFeatures(layer):
feats = []
feat = layer.GetNextFeature()
while feat:
geomf = feat.GetGeometryRef()
feats.append( { 'attributes': feat.items(), 'geom': geomf.Clone() } )
feat.Destroy()
feat = layer.GetNextFeature()
return feats
def createOutLayer(spatialRef, suffix, fields, geomType):
( path_file, ext ) = os.path.splitext( name_file )
outFile = "%s_%s%s" % ( path_file, suffix, ext )
#
if os.path.exists( outFile ):
driver.DeleteDataSource( outFile )
#
outDataSource = driver.CreateDataSource( outFile )
#
if outDataSource is None:
printStatus( "File '%s' not be created" % outFile, True )
return None
#
outLayer = outDataSource.CreateLayer( outFile, srs=spatialRef, geom_type=geomType )
#
if outLayer is None:
printStatus( "File '%s' not be created" % outFile, True )
return None
#
for item in fields:
f = ogr.FieldDefn( item['name'], item['type'] )
if item.has_key( 'width' ):
f.SetWidth( item[ 'width' ] )
outLayer.CreateField( f )
#
return ( outFile, outDataSource, outLayer )
def addFeaturesOut(f, fid, defnOut):
feat = ogr.Feature( defnOut )
feat.SetGeometry( f['geom'] )
feat.SetFID( f['fid'] )
for item in f['attributes']:
feat.SetField( item[ 'name' ], item[ 'value' ])
outLayer.CreateFeature( feat )
feat.Destroy()
def getCoordTransform7390( layer ):
wkt7390 = 'PROJCS["Brazil / Albers Equal Area Conic (WGS84)",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["longitude_of_center",-50.0],PARAMETER["standard_parallel_1",10.0],PARAMETER["standard_parallel_2",-40.0],PARAMETER["latitude_of_center",-25.0],UNIT["Meter",1.0]]'
sr7390 = osr.SpatialReference()
sr7390.ImportFromWkt( wkt7390 )
return osr.CreateCoordinateTransformation( layer.GetSpatialRef(), sr7390 )
def processNeighbour(inFeats, year, ct):
def removeFeaturesInNotConnect(inFeats):
for item in inFeats:
item['isConnect'] = False
#
idIni = 0
for item1 in inFeats:
idIni += 1
for item2 in inFeats[idIni:]:
if item1['isConnect'] and item2['isConnect']:
continue
if item1['geom'].Touches( item2['geom'] ):
item1['isConnect'] = item2['isConnect'] = True
#
for item in inFeats:
if not item['isConnect']:
item['geom'].Destroy()
item['geom'] = None
del item['isConnect']
#
return filter( lambda x: not x['geom'] is None, inFeats )
def unionFeaturesInSameDay(inFeats):
hasUnion = True
while hasUnion:
hasUnion = False
idIni = 0
for item1 in inFeats:
idIni += 1
if item1['geom'] is None:
continue
for item2 in inFeats[idIni:]:
if item2['geom'] is None:
continue
if item1['attributes']['day_of_year'] == item2['attributes']['day_of_year'] and item1['geom'].Touches( item2['geom'] ):
hasUnion = True
geom = item1['geom'].Union( item2['geom'] )
item1['geom'].Destroy()
item1['geom'] = geom.Clone()
item2['geom'].Destroy()
item2['geom'] = None
if hasUnion:
inFeats = filter( lambda x: not x['geom'] is None, inFeats )
return inFeats
def createOutFeatures(inFeats, ct):
def getValueEvent(inFeat):
day = inFeat['attributes']['day_of_year']
geom = inFeat['geom'].Clone()
geom.Transform( ct )
area = geom.Area() / 10000.0
geom.Destroy()
return {'day_of_year': day, 'area_ha': area }
def getInitValues(inFeat, id_connect):
outFeat = {}
outFeat['id_connect'] = id_connect
outFeat['geom'] = inFeat['geom'].Clone()
outFeat['events'] = [ getValueEvent( inFeat ) ]
return outFeat
def addValues(outFeat, inFeat):
geomUnion = outFeat['geom'].Union( inFeat['geom'])
outFeat['geom'].Destroy()
outFeat['geom'] = geomUnion
outFeat['events'].append( getValueEvent( inFeat ) )
# Create List Connect ( save First connection)
outFeats = []
id_connect = 0
idIni = 0
for item1 in inFeats:
idIni += 1
if item1['geom'] is None:
continue
for item2 in inFeats[idIni:]:
if item2['geom'] is None:
continue
if item1['geom'].Touches( item2['geom'] ):
id_connect += 1
outFeat = getInitValues( item1, id_connect )
addValues( outFeat, item2 )
outFeats.append( outFeat )
item1['geom'].Destroy()
item1['geom'] = None
item2['geom'].Destroy()
item2['geom'] = None
break
#
inFeats = filter( lambda x: not x['geom'] is None, inFeats )
# 2) Add Neighbours
hasUnion = True
while hasUnion:
hasUnion = False
idIni = 0
for item1 in outFeats:
idIni += 1
for item2 in inFeats:
if item2['geom'] is None:
continue
if item1['geom'].Touches( item2['geom'] ):
hasUnion = True
addValues( item1, item2 )
item2['geom'].Destroy()
item2['geom'] = None
if hasUnion:
inFeats = filter( lambda x: not x['geom'] is None, inFeats )
return ( outFeats, inFeats )
def unionFeaturesOut(outFeats):
def getSumSameDays( events ):
new_events = []
days = map( lambda x: x['day_of_year'], events )
days = list( set( days ) )
for item1 in days:
f_days = filter( lambda x: x['day_of_year'] == item1, events )
if len( f_days ) == 1:
total_area_ha = f_days[0]['area_ha']
else:
total_area_ha = 0
for item2 in f_days:
total_area_ha += item2['area_ha']
event = { 'day_of_year': item1, 'area_ha': total_area_ha }
new_events.append( event )
return new_events
# Add Neighbours
hasUnion = True
while hasUnion:
hasUnion = False
idIni = 0
for item1 in outFeats:
idIni += 1
if item1['geom'] is None:
continue
for item2 in outFeats[idIni:]:
if item2['geom'] is None:
continue
if item1['geom'].Touches( item2['geom'] ):
hasUnion = True
geomUnion = item1['geom'].Union( item2['geom'] )
item1['geom'].Destroy()
item1['geom'] = geomUnion.Clone()
item1['events'] += item2['events']
item2['geom'].Destroy()
del item2['events']
item2['geom'] = None
if hasUnion:
outFeats = filter( lambda x: not x['geom'] is None, outFeats )
# Remove duplicate day_of_year in events
for item in outFeats:
events = getSumSameDays( item['events'])
del item['events']
item['events'] = events
return outFeats
inFeats = removeFeaturesInNotConnect( inFeats )
inFeats = unionFeaturesInSameDay( inFeats )
( outFeats, inFeats ) = createOutFeatures( inFeats, ct )
outFeats = unionFeaturesOut( outFeats )
return ( outFeats, inFeats )
def getSumEvents(outFeat):
def getDate( day ):
s_jd = "%.04d%.03d" % ( year, day)
return datetime.datetime.strptime( s_jd, '%Y%j' ).strftime("%Y-%m-%d")
events = sorted( outFeat['events'], key=lambda x: x['day_of_year'] )
num_events = len( events )
day = events[0]['day_of_year']
date_ini = getDate( day )
date_end = getDate( events[num_events - 1]['day_of_year'] )
area_ini_ha = events[0]['area_ha']
#
days_events = "%03d" % day
area_end_ha = area_ini_ha
area_events_ha = "%f" % area_ini_ha
for item in events[1:]:
days_events = "%s;%03d" % ( days_events, item['day_of_year'] )
area_end_ha += item['area_ha']
area_events_ha = "%s;%f" % ( area_events_ha, item['area_ha'] )
#
return {
'num_events': num_events,
'date_ini': date_ini,
'date_end': date_end,
'area_ini_ha': area_ini_ha,
'area_end_ha': area_end_ha,
'days_events': days_events,
'area_events_ha': area_events_ha
}
ogr.RegisterAll()
driver = ogr.GetDriverByName( "GeoJSON" )
if not os.path.exists( name_file ):
printStatus( "File '%s' not exist" % name_file, True )
return 1
# Input
data = getLayer( name_file )
if data is None:
return 1
( inDataSource, inLayer ) = data
inFeats = getFeatures( inLayer )
# Output
wkrSR = inLayer.GetSpatialRef().ExportToWkt()
spatialRef = osr.SpatialReference( )
fields = [
{ 'name': 'id_connect', 'type': ogr.OFTInteger },
{ 'name': 'num_events', 'type': ogr.OFTInteger },
{ 'name': 'date_ini', 'type': ogr.OFTString, 'width': 10 },
{ 'name': 'date_end', 'type': ogr.OFTString, 'width': 10 },
{ 'name': 'area_ini_ha', 'type': ogr.OFTReal },
{ 'name': 'area_end_ha', 'type': ogr.OFTReal },
{ 'name': 'days_events', 'type': ogr.OFTString, 'width': 200 },
{ 'name': 'area_events_ha', 'type': ogr.OFTString, 'width': 200 }
]
data = createOutLayer( spatialRef, 'neighbour', fields, ogr.wkbMultiPolygon )
if data is None:
return 1
( outName, outDataSource, outLayer ) = data
defnOut = outLayer.GetLayerDefn()
# Process
data = ( outName, inLayer.GetFeatureCount() )
printStatus( "Processing '%s' (%d features)" % data , True )
( outFeats, inFeats ) = processNeighbour( inFeats, year, getCoordTransform7390( inLayer ) )
totalOutFeats = len( outFeats )
# Clean
for item in inFeats:
item['geom'].Destroy()
del inFeats[:]
# Save Features
fid = 0
for item in outFeats:
fid += 1
sumEvent = getSumEvents( item )
f = { 'fid': fid, 'geom': item['geom'],
'attributes': [
{ 'name': 'id_connect', 'value': item['id_connect'] },
{ 'name': 'num_events', 'value': sumEvent['num_events'] },
{ 'name': 'date_ini', 'value': sumEvent['date_ini'] },
{ 'name': 'date_end', 'value': sumEvent['date_end'] },
{ 'name': 'area_ini_ha', 'value': sumEvent['area_ini_ha'] },
{ 'name': 'area_end_ha', 'value': sumEvent['area_end_ha'] },
{ 'name': 'days_events', 'value': sumEvent['days_events'] },
{ 'name': 'area_events_ha', 'value': sumEvent['area_events_ha'] }
]
}
addFeaturesOut( f, fid, defnOut )
del f['attributes']
item['geom'].Destroy()
del outFeats[:]
inDataSource.Destroy()
outDataSource.Destroy()
printStatus( "Finish! '%s': Total %d features" % ( outName, totalOutFeats ), True )
return 0
def main():
parser = argparse.ArgumentParser(description='Create union neighbour polygon.')
parser.add_argument('geojson', metavar='GeoJSON', type=str, help='Name of GeoJSON file')
parser.add_argument('year', metavar='Year', type=int, help='Year for julian days')
args = parser.parse_args()
return run( args.geojson, args.year )
if __name__ == "__main__":
sys.exit( main() )