-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeoObject.py
574 lines (444 loc) · 23.2 KB
/
GeoObject.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
import os, time
from osgeo import gdal
from osgeo import osr
from osgeo import ogr
from collections import OrderedDict
import pyproj
import MetadataRecord as MDR
#--Class: geoObject -main class----
class geoObject(object):
def __init__(self, filepath):
#--Set generic system file info attributes----
self.file = os.path.basename(filepath)
self.path = os.path.dirname(filepath)
self.fullpath = filepath
self.uncpath = util.getUNCName(self.path)
self.createDate = os.path.getctime(self.fullpath)
self.modDate = os.path.getmtime(self.fullpath)
self.fileSize = os.path.getsize(self.fullpath)/1024
def xMeta(self):
#--Create ordered dictionary of generic system file info attributes----
#--for user presentable display. Could this be refactored to use only x19139Meta?
self.dsMetadata = OrderedDict()
self.dsMetadata['File Name'] = self.file
self.dsMetadata['Parent Directory'] = self.path
self.dsMetadata['File Size (mb)'] = self.fileSize
self.dsMetadata['UNC Path'] = self.uncpath
self.dsMetadata['Create Date'] = time.asctime(time.localtime(self.createDate))
self.dsMetadata['Modify Date'] = time.asctime(time.localtime(self.modDate))
return self.dsMetadata
def x19139Meta(self):
#--Create ordered dictionary of generic system file info attributes in iso19139----
self.isoMetadata = OrderedDict()
identificationInfo = OrderedDict()
self.MD_DataIdentification = OrderedDict()
citation = OrderedDict()
CI_Citation = OrderedDict()
title = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',self.file)])
CI_Citation['{http://www.isotc211.org/2005/gmd}title'] = title
citation['{http://www.isotc211.org/2005/gmd}CI_Citation'] = CI_Citation
self.MD_DataIdentification['{http://www.isotc211.org/2005/gmd}citation'] = citation
identificationInfo['{http://www.isotc211.org/2005/gmd}MD_DataIdentification'] = self.MD_DataIdentification
self.isoMetadata['{http://www.isotc211.org/2005/gmd}identificationInfo'] = identificationInfo
if self.uncpath :
fpath = self.uncpath
else:
fpath = self.path
#--Create nested ordered dictionary mirroring iso19139---
distributionInfo = OrderedDict()
self.MD_Distribution = OrderedDict()
transferOptions = OrderedDict()
MD_DigitalTransferOptions = OrderedDict()
onLine = OrderedDict()
CI_OnlineResource = OrderedDict()
linkage = OrderedDict([('{http://www.isotc211.org/2005/gmd}URL',fpath)])
CI_OnlineResource['{http://www.isotc211.org/2005/gmd}linkage'] = linkage
protocol = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',"Web address (URL)")])
CI_OnlineResource['{http://www.isotc211.org/2005/gmd}protocol'] = protocol
name = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',self.file)])
CI_OnlineResource['{http://www.isotc211.org/2005/gmd}name'] = name
description = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',self.file)])
CI_OnlineResource['{http://www.isotc211.org/2005/gmd}description'] = description
onLine['{http://www.isotc211.org/2005/gmd}CI_OnlineResource'] = CI_OnlineResource
MD_DigitalTransferOptions['{http://www.isotc211.org/2005/gmd}onLine'] = onLine
transferOptions['{http://www.isotc211.org/2005/gmd}MD_DigitalTransferOptions'] = MD_DigitalTransferOptions
self.MD_Distribution['{http://www.isotc211.org/2005/gmd}transferOptions'] = transferOptions
distributionInfo['{http://www.isotc211.org/2005/gmd}MD_Distribution'] = self.MD_Distribution
self.isoMetadata['{http://www.isotc211.org/2005/gmd}distributionInfo'] = distributionInfo
return self.isoMetadata
#--Class: RasterLayer, a subclass of geoObject----
class RasterLayer(geoObject):
def __init__(self, filepath):
#--Metadata for raster data----
geoObject.__init__(self, filepath)
raster = gdal.OpenShared(self.fullpath)
self.DataType = "Raster Data"
self.driver = raster.GetDriver().LongName
self.rasterX = raster.RasterXSize
self.rasterY = raster.RasterYSize
self.bands = self.getBands(raster)
rproj = raster.GetProjection()
if rproj:
sourceSrs = osr.SpatialReference()
sourceSrs.ImportFromWkt(rproj)
if sourceSrs is not None :
self.Projection, self.isoproj = util.getProjMData(sourceSrs)
self.Extents, self.isobox = self.getBBoxLL(raster,sourceSrs)
else:
self.Projection, self.isoproj = (None, None)
self.Extents, self.isobox = (None, None)
self.Projection = "no projection information"
del raster
def getBands(self,raster):
#--Metadata for raster bands----
resultsbands = OrderedDict()
self.BandCount = raster.RasterCount
bandsdata = OrderedDict()
bandsdata['Band Count'] = self.BandCount
for bandnum in range(self.BandCount):
band = raster.GetRasterBand(bandnum+1)
# ComputeRasterMinMax is slow for large images
# Can we speed this up?
## bmin, bmax = band.ComputeRasterMinMax()
overviews = band.GetOverviewCount()
resultseachband = OrderedDict()
resultseachband['bandId'] = str(bandnum+1)
## resultseachband['min'] = str(bmin)
## resultseachband['max'] = str(bmax)
resultseachband['Block_Size'] = band.GetBlockSize()
resultseachband['Type'] = gdal.GetDataTypeName(band.DataType)
bandColour = band.GetRasterColorInterpretation()
resultseachband['Colour'] = gdal.GetColorInterpretationName(bandColour)
resultseachband['overviews'] = str(overviews)
resultsbands[str(bandnum+1)] = resultseachband
bandsdata['band ' + str(bandnum+1)] = resultsbands[str(bandnum+1)]
return bandsdata
def getBBoxLL(self, raster, sourceSrs):
#--Bounding box metadata for raster geodata----
geotrans = raster.GetGeoTransform()
minx = geotrans[0]
maxy = geotrans[3]
maxx = minx + (geotrans[1] * self.rasterX)
miny = maxy + (geotrans[5] * self.rasterY)
extents = [minx,maxx,miny,maxy]
boundbox,isobox = util.transBBtoLL(sourceSrs,extents)
return boundbox, isobox
def xMeta(self):
#--Create ordered dictionary of raster file attributes----
#--for user presentable display. Could this be refactored to use only x19139Meta?
geoObject.xMeta(self)
self.dsMetadata['Data Type'] = self.DataType
if self.BandCount:
self.dsMetadata['Number of Bands'] = self.BandCount
if self.driver:
self.dsMetadata['File Type'] = self.driver
if self.bands:
self.dsMetadata['Raster Band'] = self.bands
if self.Projection:
self.dsMetadata['Projection'] = self.Projection
if self.Extents:
self.dsMetadata['Bounding Box'] = self.Extents
return self.dsMetadata
def x19139Meta(self):
#--Create nested ordered dictionary mirroring iso19139---
geoObject.x19139Meta(self)
distributionFormat = OrderedDict()
MD_Format = OrderedDict()
name = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',self.driver)])
MD_Format['{http://www.isotc211.org/2005/gmd}name'] = name
distributionFormat['{http://www.isotc211.org/2005/gmd}MD_Format'] = MD_Format
self.MD_Distribution['{http://www.isotc211.org/2005/gmd}distributionFormat'] = distributionFormat
self.MD_DataIdentification['{http://www.isotc211.org/2005/gmd}extent'] = self.isobox
self.isoMetadata['{http://www.isotc211.org/2005/gmd}referenceSystemInfo'] = self.isoproj
return self.isoMetadata
class VectorLayer(geoObject):
def __init__(self, filepath):
geoObject.__init__(self, filepath)
# open the vector geoData object and set DataType
vector = ogr.OpenShared(self.fullpath)
self.DataType = "Vector Data"
# retreive the data type name
self.DataFormat = vector.GetDriver().GetName()
# get the number of datalayers in the object and select the first (and only)
# To Do - set layer number as variable so as to retieve sublayers in a Geo Database
self.LayerCount = vector.GetLayerCount()
layer = vector.GetLayer(0)
self.FeatCount = str(layer.GetFeatureCount())
# Geometry type is retrieved in this section
geomType = layer.GetLayerDefn().GetGeomType() # retrieve the WKB for the feature type
if geomType != 100:
featType = ogr.Geometry(geomType) # creates a Geometry object from the WKB
self.FeatType = ogr.Geometry.ExportToWkt(featType) # creates a WKT from the Geometry object
# Retrieve the projection definition and extents
proj = layer.GetSpatialRef()
if proj is not None:
self.Projection, self.isoproj = util.getProjMData(proj)
extents = layer.GetExtent()
self.Extents,self.isobox = util.transBBtoLL(proj,extents)
else:
self.Projection = None
self.Extents = None
else:
featType = "None"
def xMeta(self):
geoObject.xMeta(self)
self.dsMetadata['Data Type'] = self.DataType
if self.DataFormat:
self.dsMetadata['Data Format'] = self.DataFormat
if self.LayerCount:
self.dsMetadata['Layer Count'] = self.LayerCount
if self.FeatCount:
self.dsMetadata['Feature Count'] = self.FeatCount
if self.FeatType:
self.dsMetadata['Feature Type'] = self.FeatType
if self.Projection:
self.dsMetadata['Projection'] = self.Projection
if self.Extents:
self.dsMetadata['Bounding Box'] = self.Extents
return self.dsMetadata
def x19139Meta(self):
#--Create nested ordered dictionary mirroring iso19139---
geoObject.x19139Meta(self)
distributionFormat = OrderedDict()
MD_Format = OrderedDict()
name = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',self.DataFormat)])
MD_Format['{http://www.isotc211.org/2005/gmd}name'] = name
distributionFormat['{http://www.isotc211.org/2005/gmd}MD_Format'] = MD_Format
self.MD_Distribution['{http://www.isotc211.org/2005/gmd}distributionFormat'] = distributionFormat
if self.isobox is not None:
self.MD_DataIdentification['{http://www.isotc211.org/2005/gmd}extent'] = self.isobox
self.isoMetadata['{http://www.isotc211.org/2005/gmd}referenceSystemInfo'] = self.isoproj
return self.isoMetadata
class geoWorkspace(geoObject):
def __init__(self, filepath):
geoObject.__init__(self, filepath)
vector = ogr.OpenShared(self.fullpath)
self.DataType = "Geodata collection"
self.LayerCount = vector.GetLayerCount()
self.subLayers = self.getSubLayers(vector)
del vector
def getSubLayers(self, vector):
subLayers = OrderedDict()
for subLayerNum in range(self.LayerCount):
subLayer = vector.GetLayer(subLayerNum)
subLayerName = subLayer.GetName()
subLayers[subLayerName] = OrderedDict()
subLayers[subLayerName]['Name'] = subLayerName
geomType = subLayer.GetLayerDefn().GetGeomType()
if geomType != 100:
featType = ogr.Geometry(geomType)
if geomType > 0:
subLayers[subLayerName]['Feature Type'] = ogr.Geometry.ExportToWkt(featType)
else:
subLayers[subLayerName]['Feature Type'] = "N/A"
subLayers[subLayerName]['FeatCount'] = str(subLayer.GetFeatureCount())
proj = subLayer.GetSpatialRef()
if proj is not None:
subLayers[subLayerName]['Projection'], iso = util.getProjMData(proj)
extents = subLayer.GetExtent()
subLayers[subLayerName]['Bounding Box'],isobox = util.transBBtoLL(proj,extents)
else:
subLayers = 0
return subLayers
def xMeta(self):
geoObject.xMeta(self)
self.dsMetadata['Data Type'] = self.DataType
if self.LayerCount:
self.dsMetadata['Layer Count'] = self.LayerCount
if self.subLayers:
self.dsMetadata['Sub Layers'] = self.subLayers
return self.dsMetadata
def x19139Meta(self):
geoObject.x19139Meta(self)
# Class: Directory - generic directory object. For objects not decernable as geodata
class Directory(geoObject):
def __init__(self, filepath):
geoObject.__init__(self, filepath)
self.DataType = 'Non-GIS formatted Directory'
def xMeta(self):
geoObject.xMeta(self)
self.dsMetadata['Data Type'] = self.DataType
return self.dsMetadata
def x19139Meta(self):
geoObject.x19139Meta(self)
class util(object): # util class includes methods not dependent on geodatatype
# getUNCName -Retrieves the UNC path of the data set selected to give
# unversal access on the network
# -- Make cross platform
def getUNCName(fullpath):
try:
import win32wnet
uncname=win32wnet.WNetGetUniversalName(fullpath,1)
except:
uncname=""
return uncname
getUNCName = staticmethod(getUNCName)
# getFileType -checks for the type of geodata selected
# returns the datatype class and icons, active and inactive
def getFileType(filepath):
datatype = ''
dsogr, dsgdal = False, False
icon1, icon2 = 'fldr', 'fldropen'
gdal.PushErrorHandler( "CPLQuietErrorHandler" )
try:
dsgdal = gdal.OpenShared(filepath)
if dsgdal is not None:
datatype = RasterLayer
icon1 = 'img'
icon2 = 'img'
else:
try:
dsogr = ogr.OpenShared(filepath)
if dsogr:
if dsogr.GetLayerCount() > 1:
datatype = geoWorkspace
icon1 = 'gdb'
icon2 = 'gdb'
elif os.path.isdir(filepath):
datatype = Directory
icon1 = 'fldr'
icon2 = 'fldropen'
else:
datatype = VectorLayer
icon1 = 'file'
icon2 = 'file'
except:
return False, icon1, icon2
except:
return False, icon1, icon2
try:
if os.path.isdir(filepath) and dsogr is None and dsgdal is None:
datatype = Directory
icon1 = 'fldr'
icon2 = 'fldropen'
except:
return False, icon1, icon2
del dsogr
del dsgdal
return datatype, icon1, icon2
getFileType = staticmethod(getFileType)
# getProjMData -retrieves projection metadata from a WKT projection string
# -- Can we connect to a live service? - low priority
# -- At least provide method of lookup (Strategy Pattern)
def getProjMData(sourceSrs):
try:
projection = OrderedDict()
projType = 'GEOGCS'
if sourceSrs.IsProjected():
projType = 'PROJCS'
name = sourceSrs.GetAttrValue(projType,0)
projection['Name'] = name
if not name == "unnamed":
authority = sourceSrs.GetAttrValue("AUTHORITY",0)
codeVal = sourceSrs.GetAttrValue("AUTHORITY",1)
if authority is None:
import epsglookup
epsg_file = os.path.join(MDR.GNConnection.projData, "epsg")
## print epsg_file
## print name
proj_lookup = epsglookup.ProjectionLookup(epsg_file)
codeVal = proj_lookup.find_by_name(name).id
authority = "epsg"
## print codeVal
projection['Authority'] = authority
projection['Code'] = codeVal
else:
authority = ""
codeVal = ""
project = sourceSrs.GetAttrValue("PROJECTION")
projection['Projection'] = project
centMer = sourceSrs.GetProjParm("central_meridian",1)
projection['Central Meridian'] = centMer
else:
name = sourceSrs.GetAttrValue(projType,0)
authority = sourceSrs.GetAttrValue("AUTHORITY",0)
codeVal = sourceSrs.GetAttrValue("AUTHORITY",1)
projection['Name'] = name
projection['Authority'] = authority
projection['Code'] = codeVal
isoproj = OrderedDict()
referenceSystemInfo = OrderedDict()
MD_ReferenceSystem = OrderedDict()
referenceSystemIdentifier = OrderedDict()
RS_Identifier = OrderedDict()
if (authority) and (codeVal):
code = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',codeVal)])
RS_Identifier['{http://www.isotc211.org/2005/gmd}code'] = code
auth = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',authority)])
RS_Identifier['{http://www.isotc211.org/2005/gmd}codeSpace'] = auth
else :
code = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',name)])
RS_Identifier['{http://www.isotc211.org/2005/gmd}code'] = code
referenceSystemIdentifier['{http://www.isotc211.org/2005/gmd}RS_Identifier'] = RS_Identifier
MD_ReferenceSystem['{http://www.isotc211.org/2005/gmd}referenceSystemIdentifier'] = referenceSystemIdentifier
referenceSystemInfo['{http://www.isotc211.org/2005/gmd}MD_ReferenceSystem'] = MD_ReferenceSystem
isoproj['{http://www.isotc211.org/2005/gmd}referenceSystemInfo'] = referenceSystemInfo
except:
projection = "Faulty projection parameters"
isoproj = None
return projection, isoproj
getProjMData = staticmethod(getProjMData)
# transBBtoLL -Translates a list of coordinate values (minx, maxx, miny, maxy)
# from native projection to WGS84 lat long coordinates
def transBBtoLL(sourceSrs,extents):
try:
minx = extents[0]
maxx = extents[1]
miny = extents[2]
maxy = extents[3]
newsrs = sourceSrs.ExportToProj4()
# The following lines have been replaced to use pyproj instead of osr due
# to the current flakiness of TransformPoint not acceptint arguements.
# i.e. (NotImplementedError: Wrong number of arguments for overloaded function
# 'CoordinateTransformation_TransformPoint'.)
## targetSrs = osr.SpatialReference()
## targetSrs.ImportFromEPSG(4326)
##
## transform = osr.CoordinateTransformation(sourceSrs,targetSrs)
##
## ulcoord = transform.TransformPoint(minx, maxy, 0.0)
## llcoord = transform.TransformPoint(minx, miny, 0.0)
## lrcoord = transform.TransformPoint(maxx, miny, 0.0)
## urcoord = transform.TransformPoint(maxx, maxy, 0.0)
boundbox = OrderedDict()
if sourceSrs.GetAttrValue("AUTHORITY",1) == "4326" and not sourceSrs.IsProjected():
boundbox['maxx'] = maxx
boundbox['maxy'] = maxy
boundbox['minx'] = minx
boundbox['miny'] = miny
else:
# pyproj section
pyproj.set_datapath(MDR.GNConnection.projData)
pyproj.pyproj_datadir = MDR.GNConnection.projData
pyproj.set_datapath(MDR.GNConnection.projData)
p1 = pyproj.Proj(newsrs)
p2 = pyproj.Proj(init='epsg:4326')
ulcoord = pyproj.transform(p1, p2, minx, maxy)
lrcoord = pyproj.transform(p1, p2, maxx, miny)
llcoord = pyproj.transform(p1, p2, minx, miny)
urcoord = pyproj.transform(p1, p2, maxx, maxy)
# end pyproj section
boundbox['maxx'] = max(lrcoord[0], urcoord[0])
boundbox['maxy'] = max(ulcoord[1], urcoord[1])
boundbox['minx'] = min(llcoord[0], ulcoord[0])
boundbox['miny'] = min(llcoord[1], lrcoord[1])
extent = OrderedDict()
EX_Extent = OrderedDict()
geographicElement = OrderedDict()
EX_GeographicBoundingBox = OrderedDict()
minx = OrderedDict([('{http://www.isotc211.org/2005/gco}Decimal',boundbox['minx'])])
EX_GeographicBoundingBox['{http://www.isotc211.org/2005/gmd}westBoundLongitude'] = minx
maxx = OrderedDict([('{http://www.isotc211.org/2005/gco}Decimal',boundbox['maxx'])])
EX_GeographicBoundingBox['{http://www.isotc211.org/2005/gmd}eastBoundLongitude'] = maxx
miny = OrderedDict([('{http://www.isotc211.org/2005/gco}Decimal',boundbox['miny'])])
EX_GeographicBoundingBox['{http://www.isotc211.org/2005/gmd}southBoundLatitude'] = miny
maxy = OrderedDict([('{http://www.isotc211.org/2005/gco}Decimal',boundbox['maxy'])])
EX_GeographicBoundingBox['{http://www.isotc211.org/2005/gmd}northBoundLatitude'] = maxy
geographicElement['{http://www.isotc211.org/2005/gmd}EX_GeographicBoundingBox'] = EX_GeographicBoundingBox
EX_Extent['{http://www.isotc211.org/2005/gmd}geographicElement'] = geographicElement
extent['{http://www.isotc211.org/2005/gmd}EX_Extent'] = EX_Extent
except:
boundbox = "Faulty proj4 parameters"
extent = None
return boundbox, extent
transBBtoLL = staticmethod(transBBtoLL)