[GRASS-SVN] r66643 - grass-addons/grass7/general/g.proj.identify

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 28 10:01:39 PDT 2015


Author: krejcmat
Date: 2015-10-28 10:01:39 -0700 (Wed, 28 Oct 2015)
New Revision: 66643

Modified:
   grass-addons/grass7/general/g.proj.identify/g.proj.identify.py
Log:
addons g.proj.identify: add flag -s for writing EPSG to PERMANENT mapset

Modified: grass-addons/grass7/general/g.proj.identify/g.proj.identify.py
===================================================================
--- grass-addons/grass7/general/g.proj.identify/g.proj.identify.py	2015-10-28 16:39:33 UTC (rev 66642)
+++ grass-addons/grass7/general/g.proj.identify/g.proj.identify.py	2015-10-28 17:01:39 UTC (rev 66643)
@@ -43,11 +43,50 @@
 #% description: Print WKT
 #%end
 
+#%flag
+#% key: s
+#% label: save
+#% description: Save as default EPSG
+#%end
 from grass.script import core as grass
 from grass.pygrass.modules import Module
 from subprocess import PIPE
 from osgeo import osr
+import os
 
+
+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()
+                print("EPSG code have been written to < %s >"%path)
+            except IOError as e:
+                print "I/O error({0}): {1}".format(e.errno, e.strerror)
+
+        else:
+            print("EPSG file already exist < %s >"%path)
+    else:
+        try:
+            io = open(path,'w')
+            io.write("epsg: %s"%epsg)
+            io.close()
+            print("EPSG code have been written to < %s >"%path)
+        except IOError as e:
+            print "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',
@@ -59,6 +98,11 @@
         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("Cannot acces PERMANENT mapset")
             return
     try:
         proj=Module('g.proj',
@@ -80,7 +124,13 @@
     srs.AutoIdentifyEPSG()
     try :
         int(srs.GetAuthorityCode(None))
-        print('epsg=%s' % srs.GetAuthorityCode(None))
+        epsg=srs.GetAuthorityCode(None)
+        print('epsg=%s' % epsg)
+        if flags['s']:
+            if isPermanent():
+                writeEPSGtoPEMANENT(epsg)
+            else:
+                grass.warning("Cannot acces PERMANENT mapset")
     except:
         grass.error('Epsg code cannot be identified')
 



More information about the grass-commit mailing list