-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathg.proj.identify.py
177 lines (149 loc) · 4.61 KB
/
g.proj.identify.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
#!/usr/bin/env python3
# -*- coding: utf-8
"""
@module g.proj.identify
@brief Module for automatic identification of EPSG from definition of projection in WKT format.
(C) 2014-2015 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Matej Krejci <matejkrejci gmail.com> (GSoC 2015)
"""
# %module
# % description: Autoidentifies EPSG code from WKT CRS definition.
# % keyword: general
# % keyword: projection
# % keyword: EPSG
# % keyword: WKT
# % keyword: .prj
# %end
# %option G_OPT_F_INPUT
# % key: wkt
# % description: Name of input file with WKT definition
# % required: no
# %end
# %option
# % key: epsg
# % type: integer
# % description: Input EPSG code
# % required: no
# %end
# %flag
# % key: p
# % description: Print projection info in Proj4 format
# %end
# %flag
# % key: w
# % description: Print projection info in WKT format
# %end
# %flag
# % key: s
# % description: Save as default EPSG in the current location
# %end
import os
from subprocess import PIPE
from grass.script import core as grass
from grass.pygrass.modules import Module
def writeEPSGtoPEMANENT(epsg):
env = grass.gisenv()
gisdbase = env["GISDBASE"]
location = env["LOCATION_NAME"]
path = os.path.join(gisdbase, location, "PERMANENT", "PROJ_EPSG")
if os.path.isfile(path): # if already file exist
if os.getenv("GRASS_OVERWRITE", False):
try:
io = open(path, "w")
io.write("epsg: %s" % epsg)
io.close()
grass.message("EPSG code have been written to <%s>" % path)
except OSError as e:
grass.error("I/O error({0}): {1}".format(e.errno, e.strerror))
else:
grass.message("EPSG file already exist <%s>" % path)
else:
try:
io = open(path, "w")
io.write("epsg: %s" % epsg)
io.close()
grass.message("EPSG code have been written to <%s>" % path)
except OSError as e:
grass.error("I/O error({0}): {1}".format(e.errno, e.strerror))
def isPermanent():
env = grass.gisenv()
if env["MAPSET"] == "PERMANENT":
return True
return False
def grassEpsg():
proj = Module("g.proj", flags="p", quiet=True, stdout_=PIPE)
proj = proj.outputs.stdout
lines = proj.splitlines()
for e, line in enumerate(lines):
if "EPSG" in line:
epsg = lines[e + 1].split(":")[1].replace(" ", "")
print("epsg=%s" % epsg)
if flags["s"]:
if isPermanent():
writeEPSGtoPEMANENT(epsg)
else:
grass.warning("Unable to access PERMANENT mapset")
return
try:
proj = Module("g.proj", flags="wf", quiet=True, stdout_=PIPE)
proj = proj.outputs.stdout
wkt2standards(proj)
except:
grass.error("WKT input error")
def wkt2standards(prj_txt):
srs = osr.SpatialReference()
srs.ImportFromESRI([prj_txt])
if flags["w"]:
print("wkt=%s" % srs.ExportToWkt())
if flags["p"]:
print("proj4=%s" % srs.ExportToProj4())
srs.AutoIdentifyEPSG()
try:
int(srs.GetAuthorityCode(None))
epsg = srs.GetAuthorityCode(None)
print("epsg=%s" % epsg)
if flags["s"]:
if isPermanent():
writeEPSGtoPEMANENT(epsg)
else:
grass.warning("Unable to access PERMANENT mapset")
except:
grass.error("EPSG code cannot be identified")
def epsg2standards(epsg):
srs = osr.SpatialReference()
srs.ImportFromEPSG(int(epsg))
if flags["w"]:
print("wkt=%s" % srs.ExportToWkt())
if flags["p"]:
print("proj4=%s" % srs.ExportToProj4())
def main():
try:
from osgeo import osr
except ImportError:
grass.fatal(
_(
"Unable to load GDAL Python bindings (requires package "
"'python-gdal' being installed)"
),
)
epsg = options["epsg"]
pathwkt = options["wkt"]
if epsg and pathwkt:
grass.error("Only one type of conversions can be processed concurrently")
if epsg:
epsg2standards(epsg)
else:
if pathwkt:
try:
io = open(pathwkt, "r")
wkt = io.read().rstrip()
wkt2standards(wkt)
except OSError as e:
grass.error("Unable to open file <%s>: %s" % (e.errno, e.strerror))
else:
grassEpsg()
if __name__ == "__main__":
options, flags = grass.parser()
main()