-
Notifications
You must be signed in to change notification settings - Fork 12
/
morps.py
622 lines (477 loc) · 18.6 KB
/
morps.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
try:
from osgeo import ogr
except ImportError:
import ogr
import sys, subprocess, os
driverList = {"shp":"ESRI Shapefile","json":"GeoJSON","kml":"KML"}
### TO DO:
### Retain attribute information when creating new files
def unitConvert(un,deg):
if un=="km":
deg=deg/111.325
return deg
elif un=="mi":
deg=deg/111.325
deg=deg/.621371
return deg
elif un=="m":
deg=deg/111.325
deg=deg/1000
return deg
elif un=="ft":
deg=deg/111.325
deg=deg/.621371
deg=deg/5280
return deg
elif un=="deg":
return deg
else:
print "Inocorrect Units - distance will be in 'degrees'"
return deg
def featureCount(inputFile,fileType="shp"):
#return the number of features in a dataset
driver = ogr.GetDriverByName(driverList[fileType])
inputDS = driver.Open(inputFile,0)
layer = inputDS.GetLayer()
return layer.GetFeatureCount()
### This function creates a buffer for any file passed it, point, line, or
### polygon. It takes four arguments, InputFileName, for the file with which
### to create a buffer, OutFileName, which is the output file name, buf,
### which is specifying the size of the buffer, and fileType, which should be
### the type of the file being processed.
def buffer(InputFileName,OutFileName,buf,units,fileType="shp"):
buf = unitConvert(units,buf)
#get fields from input file
inputFields = getFields(InputFileName,fileType)
OutputFileName = OutFileName
driver = ogr.GetDriverByName(driverList[fileType])
inputDS = driver.Open(InputFileName, 0)
if inputDS is None:
print 'Could not open input file',InputFileName
inputLayer = inputDS.GetLayer()
if os.path.exists(OutputFileName):
os.remove(OutputFileName)
try:
outputDS = driver.CreateDataSource(OutputFileName)
except:
print 'Could not create output file',OutputFileName
newLayer = outputDS.CreateLayer(OutputFileName, geom_type=ogr.wkbPolygon,srs=inputLayer.GetSpatialRef())
if newLayer is None:
print "Could not create layer for buffer in output data source"
##testing- add fields from input dataset to output dataset
try:
for field in inputFields:
fieldDefinition = ogr.FieldDefn(field,inputFields[field])
#if field is type string
if inputFields[field] == 4:
fieldDefinition.setWidth(250)
newLayer.CreateField(fieldDefinition)
except:
print 'ur fuct'
newLayerDef = newLayer.GetLayerDefn()
featureID = 0
oldFeature = inputLayer.GetNextFeature()
while oldFeature:
geometry = oldFeature.GetGeometryRef()
bufferz = geometry.Buffer(buf,5)
try:
newFeature = ogr.Feature(newLayerDef)
newFeature.SetGeometry(bufferz)
newFeature.SetFID(featureID)
newLayer.CreateFeature(newFeature)
except:
print "Error adding buffer"
newFeature.Destroy()
oldFeature.Destroy()
oldFeature = inputLayer.GetNextFeature()
featureID += 1
print 'There are', featureID, 'input features'
inputDS.Destroy()
outputDS.Destroy()
### This function creates centroids for any file passed to it
### It takes three arguments- inputFile, for the file with which
### to derive the centroids; outFile, which is the name of the new centroid
### file, and fileType, which is the type of file being passed (e.g., 'shp')
def centroid(inputFile,outFile,fileType="shp"):
outputFileName = outFile
driver = ogr.GetDriverByName(driverList[fileType])
inputDS = ogr.Open(inputFile,0)
if inputDS is None:
print "Could not open input file", inputFile
layer = inputDS.GetLayer()
#test if input is point geometry
if layer.GetGeomType() == 1:
#exit the function! why are you getting centroids of points??
return "You submitted a point dataset. Please submit a dataset containing line or polygon geometries."
#create output file
if os.path.exists(outputFileName):
os.remove(outputFileName)
try:
outputDS = driver.CreateDataSource(outputFileName)
except:
print 'Could not create output file', outputDS
newLayer = outputDS.CreateLayer('centroid',geom_type=ogr.wkbPoint,srs=layer.GetSpatialRef())
if newLayer is None:
print "Couldn't create layer for buffer in output DS"
newLayerDef = newLayer.GetLayerDefn()
featureID = 0
oldFeature = layer.GetNextFeature()
while oldFeature:
geometry = oldFeature.GetGeometryRef()
centroid = geometry.Centroid()
try:
newFeature = ogr.Feature(newLayerDef)
newFeature.SetGeometry(centroid)
newFeature.SetFID(featureID)
newLayer.CreateFeature(newFeature)
except:
print "Error computing centroid for feature", featureID
newFeature.Destroy()
oldFeature.Destroy()
oldFeature = layer.GetNextFeature()
featureID += 1
inputDS.Destroy()
outputDS.Destroy()
### This function checks two feature geometries in a file to see if one contains another.
### It takes 4 arguments, f1 for the first file, fid1 for the objectID of the
### first file's feature, f2 for the second file, fid2 for the objectID of the
### second file's feature. Returns whether the containment is True or False.
def contains(f1,f2,fid1=0,fid2=0,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
file1 = driver.Open(f1,0)
layer1 = file1.GetLayer()
feat1 = layer1.GetFeature(fid1)
geom1 = feat1.GetGeometryRef()
file2 = driver.Open(f2,0)
layer2 = file2.GetLayer()
feat2 = layer2.GetFeature(fid2)
geom2 = feat2.GetGeometryRef()
if geom1.Contains(geom2) == 1:
return True
else:
return False
def difference(f1,f2,outFile,fileType="shp"):
outputFileName = outFile
driver = ogr.GetDriverByName(driverList[fileType])
f1 = driver.Open(f1,0)
layer1 = f1.GetLayer()
feature1 = layer1.GetNextFeature()
if f1 is None:
print "Could not open file ", f1
f2 = driver.Open(f2,0)
layer2 = f2.GetLayer()
feature2 = layer2.GetNextFeature()
if f2 is None:
print "Could not open file ", f2
### Create output file ###
if os.path.exists(outputFileName):
os.remove(outputFileName)
try:
output = driver.CreateDataSource(outputFileName)
except:
print 'Could not create output datasource ', outputFileName
newLayer = output.CreateLayer('SymmetricDifference',geom_type=ogr.wkbPolygon,srs=layer1.GetSpatialRef())
if newLayer is None:
print "Could not create output layer"
newLayerDef = newLayer.GetLayerDefn()
##############################
featureID = 0
while feature1:
layer2.ResetReading()
geom1 = feature1.GetGeometryRef()
feature2 = layer2.GetNextFeature()
while feature2:
geom2 = feature2.GetGeometryRef()
if geom1.Overlaps(geom2) == 1:
newgeom = geom1.Difference(geom2)
newFeature = ogr.Feature(newLayerDef)
newFeature.SetGeometry(newgeom)
newFeature.SetFID(featureID)
newLayer.CreateFeature(newFeature)
featureID += 1
newFeature.Destroy()
else:
newFeature1 = ogr.Feature(newLayerDef)
newFeature1.SetGeometry(geom1)
newFeature1.SetFID(featureID)
newLayer.CreateFeature(newFeature1)
featureID += 1
newFeature2 = ogr.Feature(newLayerDef)
newFeature2.SetGeometry(geom2)
newFeature2.SetFID(featureID)
newLayer.CreateFeature(newFeature2)
featureID += 1
newFeature1.Destroy()
newFeature2.Destroy()
feature2.Destroy()
feature2 = layer2.GetNextFeature()
feature1.Destroy()
feature1 = layer1.GetNextFeature()
f1.Destroy()
f2.Destroy()
print "Success"
return
### This function checks two features in a file to see if one touches another.
### It takes 4 arguments, f1 for the first file, fid1 for the index of the
### first file's feature, f2 for the second file, fid2 for the index of the
### second file's feature. Returns whether touch is True or False.
def disjoint(f1,f2,fid1=0,fid2=0,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
file1 = driver.Open(f1,0)
layer1 = file1.GetLayer()
feat1 = layer1.GetFeature(fid1)
geom1 = feat1.GetGeometryRef()
file2 = driver.Open(f2,0)
layer2 = file2.GetLayer()
feat2 = layer2.GetFeature(fid2)
geom2 = feat2.GetGeometryRef()
if geom1.Disjoint(geom2) == 0:
return False
else:
return True
### This function returns the distance between two geometries.
### It takes 4 arguments, f1 for the first file, fid1 for the index of the
### first file's geometry, f2 for the second file, fid2 for the index of the
### second file's geometry.
def distance(f1,f2,fid1=0,fid2=0,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
file1 = driver.Open(f1,0)
layer1 = file1.GetLayer()
feat1 = layer1.GetFeature(fid1)
geom1 = feat1.GetGeometryRef()
file2 = driver.Open(f2,0)
layer2 = file2.GetLayer()
feat2 = layer2.GetFeature(fid2)
geom2 = feat2.GetGeometryRef()
if geom1.Distance(geom2) == -1:
print 'An error occurred when attempting to compute the Distance'
return geom1.Distance(geom2)
else:
return geom1.Distance(geom2)
### This function checks two features in a file to see their geometries are
### equal. It takes 4 arguments, f1 for the first file, fid1 for the index of the
### first file's geometry, f2 for the second file, fid2 for the index of the
### second file's geometry. Returns a boolean depending on whether the geometries
### are equal.
def equals(f1,f2,fid1=0,fid2=0,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
file1 = driver.Open(f1,0)
layer1 = file1.GetLayer()
feat1 = layer1.GetFeature(fid1)
geom1 = feat1.GetGeometryRef()
file2 = driver.Open(f2,0)
layer2 = file2.GetLayer()
feat2 = layer2.GetFeature(fid2)
geom2 = feat2.GetGeometryRef()
if geom1.Equals(geom2) == 1:
return True
else:
return False
### This returns the bounding envelope for a specific geometry. See layerExtent.py
### to get the entire extent of a layer
def getEnvelope(inFile,g=0,fileType="shp"): #inFile = file, g = geometry
driver = ogr.GetDriverByName(driverList[fileType])
f = driver.Open(inFile,0)
layer = f.GetLayer()
featCount = layer.GetFeatureCount()
if g > featCount:
print "Feature ", g, "does not exist in", inFile
try:
feature = layer.GetFeature(g)
geom = feature.GetGeometryRef()
envelope = geom.GetEnvelope()
return envelope
except:
print "Could not obtain envelope"
### This program returns all fields within a specific file. It takes one
### argument, f, for the name of the file with which the fields are desired
### to be known.
def getFields(f,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
inFile = driver.Open(f,0)
if inFile is None:
print 'Could not open file', f, 'to read fields'
layer = inFile.GetLayer()
layerDefinition = layer.GetLayerDefn()
fieldList = {}
try:
for i in range(layerDefinition.GetFieldCount()):
fieldName = layerDefinition.GetFieldDefn(i).GetName()
fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType()
fieldList[fieldName] = fieldTypeCode
return fieldList
except:
print "Unable to read fields from", f
### This function checks two features in a file to see if they intersect.
### It takes 4 arguments, f1 for the first file, fid1 for the index of the
### first file's feature, f2 for the second file, fid2 for the index of the
### second file's feature. Returns whether the intersection is True or False.
def intersect(f1,f2,fid1=0,fid2=0,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
file1 = driver.Open(f1,0)
layer1 = file1.GetLayer()
feat1 = layer1.GetFeature(fid1)
geom1 = feat1.GetGeometryRef()
file2 = driver.Open(f2,0)
layer2 = file2.GetLayer()
feat2 = layer2.GetFeature(fid2)
geom2 = feat2.GetGeometryRef()
if geom1.Intersect(geom2) == 1:
return True
else:
return False
### This returns the extent of the entire layer. See getEnvelope.py
### to get the bounding envelope for specific geometries
def layerExtent(f,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
f = driver.Open(f,0)
layer = f.GetLayer()
extent = layer.GetExtent()
return extent
###tolerance: the distance tolerance for the simplification
def simplify(infile,outFile,tolerance, fileType="shp"):
outFileName = outFile
driver = ogr.GetDriverByName(driverList[fileType])
infile = driver.Open(infile,0)
if infile is None:
print 'Could not open file ', infile
sys.exit(1)
oldLayer = infile.GetLayer()
oldFeature = oldLayer.GetNextFeature()
geom = oldFeature.GetGeometryRef()
geomType = geom.GetGeometryType()
########Create output file############
if os.path.exists(outFileName):
os.remove(outFileName)
try:
output = driver.CreateDataSource(outFileName)
except:
print 'Could not create output file', outFileName
sys.exit(1)
newLayer = output.CreateLayer('Tolerance',geom_type=geomType,srs=oldLayer.GetSpatialRef())
if newLayer is None:
print 'Could not create layer for simplify in output file'
sys.exit(1)
newLayerDef = newLayer.GetLayerDefn()
#######################################
featureID = 0
####### Simplify geometry and add to output file #######
while oldFeature:
geometry = oldFeature.GetGeometryRef()
simplifiedGeom = geometry.Simplify(tolerance)
try:
newFeature = ogr.Feature(newLayerDef)
newFeature.SetGeometry(simplifiedGeom)
newFeature.SetFID(featureID)
newLayer.CreateFeature(newFeature)
except:
print "Error performing simplify"
newFeature.Destroy()
oldFeature.Destroy()
oldFeature = oldLayer.GetNextFeature()
featureID += 1
##########################################################
infile.Destroy()
output.Destroy()
print "Success"
return
def symmetricDifference(f1,f2,outFile,fileType="shp"):
outputFileName = outFile
driver = ogr.GetDriverByName(driverList[fileType])
f1 = driver.Open(f1,0)
layer1 = f1.GetLayer()
feature1 = layer1.GetNextFeature()
if f1 is None:
print "Could not open file ", f1
sys.exit(1)
f2 = driver.Open(f2,0)
layer2 = f2.GetLayer()
feature2 = layer2.GetNextFeature()
if f2 is None:
print "Could not open file ", f2
### Create output file ###
if os.path.exists(outputFileName):
os.remove(outputFileName)
try:
output = driver.CreateDataSource(outputFileName)
except:
print 'Could not create output datasource ', outputFileName
sys.exit(1)
newLayer = output.CreateLayer('SymmetricDifference',geom_type=ogr.wkbPolygon,srs=layer1.GetSpatialRef())
if newLayer is None:
print "Could not create output layer"
sys.exit(1)
newLayerDef = newLayer.GetLayerDefn()
##############################
featureID = 0
while feature1:
layer2.ResetReading()
geom1 = feature1.GetGeometryRef()
feature2 = layer2.GetNextFeature()
while feature2:
geom2 = feature2.GetGeometryRef()
if geom1.Overlaps(geom2) == 1:
newgeom = geom1.SymmetricDifference(geom2)
newFeature = ogr.Feature(newLayerDef)
newFeature.SetGeometry(newgeom)
newFeature.SetFID(featureID)
newLayer.CreateFeature(newFeature)
featureID += 1
newFeature.Destroy()
else:
newFeature1 = ogr.Feature(newLayerDef)
newFeature1.SetGeometry(geom1)
newFeature1.SetFID(featureID)
newLayer.CreateFeature(newFeature1)
featureID += 1
newFeature2 = ogr.Feature(newLayerDef)
newFeature2.SetGeometry(geom2)
newFeature2.SetFID(featureID)
newLayer.CreateFeature(newFeature2)
featureID += 1
newFeature1.Destroy()
newFeature2.Destroy()
feature2.Destroy()
feature2 = layer2.GetNextFeature()
feature1.Destroy()
feature1 = layer1.GetNextFeature()
f1.Destroy()
f2.Destroy()
print "Success"
return
### This program returns a list of all unique values within a specified
### field. It takes two arguments, 'f' for the filename (including extension)
### and 'field' for the desired field. Both arguments passed to the function
### should be strings.
def uniqueValues(f,field,fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
inFile = driver.Open(f,0)
layer = inFile.GetLayer()
noExt = f[:-4]
uniqueValues = "select distinct " + field + " from " + noExt
result = inFile.ExecuteSQL(uniqueValues)
resultFeat = result.GetNextFeature()
uniqueFieldList = []
while resultFeat:
field = resultFeat.GetField(0)
uniqueFieldList.append(field)
resultFeat = result.GetNextFeature()
print uniqueFieldList
### This function checks two features in a file to see if one is within another.
### It takes 4 arguments, f1 for the first file, fid1 for the index of the
### first file's feature, f2 for the second file, fid2 for the index of the
### second file's feature. Returns whether the within is True or False.
def within(f1,f2,fid1=0,fid2=0, fileType="shp"):
driver = ogr.GetDriverByName(driverList[fileType])
file1 = driver.Open(f1,0)
layer1 = file1.GetLayer()
feat1 = layer1.GetFeature(fid1)
geom1 = feat1.GetGeometryRef()
file2 = driver.Open(f2,0)
layer2 = file2.GetLayer()
feat2 = layer2.GetFeature(fid2)
geom2 = feat2.GetGeometryRef()
if geom1.Within(geom2) == 1:
return True
else:
return False