Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. lyr = conn.GetLayerByName(tbl) # Where conn is OGR PG connection
  2. srs = ly.GetSpatialRef()
  3. print srs
  4.  
  5. PROJCS["OSGB 1936 / British National Grid",
  6. GEOGCS["OSGB 1936",
  7. DATUM["OSGB_1936",
  8. SPHEROID["Airy 1830",6377563.396,299.3249646,
  9. AUTHORITY["EPSG","7001"]],
  10. AUTHORITY["EPSG","6277"]],
  11. PRIMEM["Greenwich",0,
  12. AUTHORITY["EPSG","8901"]],
  13. UNIT["degree",0.01745329251994328,
  14. AUTHORITY["EPSG","9122"]],
  15. AUTHORITY["EPSG","4277"]],
  16. UNIT["metre",1,
  17. AUTHORITY["EPSG","9001"]],
  18. PROJECTION["Transverse_Mercator"],
  19. PARAMETER["latitude_of_origin",49],
  20. PARAMETER["central_meridian",-2],
  21. PARAMETER["scale_factor",0.9996012717],
  22. PARAMETER["false_easting",400000],
  23. PARAMETER["false_northing",-100000],
  24. AUTHORITY["EPSG","27700"],
  25. AXIS["Easting",EAST],
  26. AXIS["Northing",NORTH]]
  27.  
  28. srs.GetEPSG()
  29. print srs
  30. 27700
  31.  
  32. In [1]: import osgeo.osr as osr
  33.  
  34. In [2]: srs = osr.SpatialReference()
  35.  
  36. In [3]: srs.SetFromUserInput("EPSG:27700")
  37. Out[3]: 0
  38.  
  39. In [4]: print srs
  40. PROJCS["OSGB 1936 / British National Grid",
  41. GEOGCS["OSGB 1936",
  42. DATUM["OSGB_1936",
  43. SPHEROID["Airy 1830",6377563.396,299.3249646,
  44. AUTHORITY["EPSG","7001"]],
  45. TOWGS84[375,-111,431,0,0,0,0],
  46. AUTHORITY["EPSG","6277"]],
  47. PRIMEM["Greenwich",0,
  48. AUTHORITY["EPSG","8901"]],
  49. UNIT["degree",0.0174532925199433,
  50. AUTHORITY["EPSG","9122"]],
  51. AUTHORITY["EPSG","4277"]],
  52. PROJECTION["Transverse_Mercator"],
  53. PARAMETER["latitude_of_origin",49],
  54. PARAMETER["central_meridian",-2],
  55. PARAMETER["scale_factor",0.9996012717],
  56. PARAMETER["false_easting",400000],
  57. PARAMETER["false_northing",-100000],
  58. UNIT["metre",1,
  59. AUTHORITY["EPSG","9001"]],
  60. AXIS["Easting",EAST],
  61. AXIS["Northing",NORTH],
  62. AUTHORITY["EPSG","27700"]]
  63.  
  64. In [5]: srs.GetAttrValue("AUTHORITY", 0)
  65. Out[5]: 'EPSG'
  66.  
  67. In [6]: srs.GetAttrValue("AUTHORITY", 1)
  68. Out[6]: '27700'
  69.  
  70. In [12]: srs.GetAttrValue("PRIMEM|AUTHORITY", 1)
  71. Out[12]: '8901'
  72.  
  73. In [13]: srs.GetAttrValue("PROJCS|GEOGCS|AUTHORITY", 1)
  74. Out[13]: '4277'
  75.  
  76. def wkt2epsg(wkt, epsg='/usr/local/share/proj/epsg', forceProj4=False):
  77. ''' Transform a WKT string to an EPSG code
  78.  
  79. Arguments
  80. ---------
  81.  
  82. wkt: WKT definition
  83. epsg: the proj.4 epsg file (defaults to '/usr/local/share/proj/epsg')
  84. forceProj4: whether to perform brute force proj4 epsg file check (last resort)
  85.  
  86. Returns: EPSG code
  87.  
  88. '''
  89. code = None
  90. p_in = osr.SpatialReference()
  91. s = p_in.ImportFromWkt(wkt)
  92. if s == 5: # invalid WKT
  93. return None
  94. if p_in.IsLocal() == 1: # this is a local definition
  95. return p_in.ExportToWkt()
  96. if p_in.IsGeographic() == 1: # this is a geographic srs
  97. cstype = 'GEOGCS'
  98. else: # this is a projected srs
  99. cstype = 'PROJCS'
  100. an = p_in.GetAuthorityName(cstype)
  101. ac = p_in.GetAuthorityCode(cstype)
  102. if an is not None and ac is not None: # return the EPSG code
  103. return '%s:%s' %
  104. (p_in.GetAuthorityName(cstype), p_in.GetAuthorityCode(cstype))
  105. else: # try brute force approach by grokking proj epsg definition file
  106. p_out = p_in.ExportToProj4()
  107. if p_out:
  108. if forceProj4 is True:
  109. return p_out
  110. f = open(epsg)
  111. for line in f:
  112. if line.find(p_out) != -1:
  113. m = re.search('<(\d+)>', line)
  114. if m:
  115. code = m.group(1)
  116. break
  117. if code: # match
  118. return 'EPSG:%s' % code
  119. else: # no match
  120. return None
  121. else:
  122. return None
  123.  
  124. In [1]: import osgeo.osr as osr
  125.  
  126. In [2]: srs = osr.SpatialReference()
  127.  
  128. In [3]: srs.SetFromUserInput("EPSG:27700")
  129. Out[3]: 0
  130.  
  131. In [4]: srs.GetAuthorityCode(None)
  132. Out[4]: '27700'
  133.  
  134. In [5]: srs.GetAuthorityCode(None)
  135. Out[5]: 'EPSG'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement