[GRASS-SVN] r59310 - grass-addons/grass7/vector/v.in.geopaparazzi

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Mar 24 09:02:22 PDT 2014


Author: lucadelu
Date: 2014-03-24 09:02:22 -0700 (Mon, 24 Mar 2014)
New Revision: 59310

Modified:
   grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.html
   grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.py
Log:
v.in.geopaparazzi: cleanup PEP8, fix problems discovered by Hayashi-san and Venka-san, update documentation

Modified: grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.html
===================================================================
--- grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.html	2014-03-24 15:33:48 UTC (rev 59309)
+++ grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.html	2014-03-24 16:02:22 UTC (rev 59310)
@@ -2,9 +2,23 @@
 
 <em>v.in.geopaparazzi</em> imports all elements saved into Geopaparazzi. You can import bookmarks, images (warning the path to images it's relative of Android device), notes (one layer for each category) and tracks. 
 
+<h2>EXAMPLES</h2>
+
+To import all the elements in the Geopaparazzi database you have to use:
+
+<div class="code"><pre>
+v.in.geopaparazzi -bint database=/path/to/your/geopaparazzi.db base=mydata
+</pre></div>
+
+To import only the tracks in 3D format
+
+<div class="code"><pre>
+v.in.geopaparazzi -tz database=/path/to/your/geopaparazzi.db base=track3d
+</pre></div>
+
 <h2>REFERENCES</h2>
 
-<a href="http://code.google.com/p/geopaparazzi/">Geopaparazzi</a>
+<a href="http://geopaparazzi.github.io/geopaparazzi/">Geopaparazzi</a>
 <p>
 <a href="http://code.google.com/p/geopaparazzi/wiki/DbTables">Geopaparazzi Tables schema</a>
 

Modified: grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.py
===================================================================
--- grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.py	2014-03-24 15:33:48 UTC (rev 59309)
+++ grass-addons/grass7/vector/v.in.geopaparazzi/v.in.geopaparazzi.py	2014-03-24 16:02:22 UTC (rev 59310)
@@ -18,7 +18,7 @@
 #% keywords: vector
 #%end
 #%flag
-#% key: b 
+#% key: b
 #% description: Import bookmarks
 #%end
 #%flag
@@ -49,16 +49,16 @@
 #% required : yes
 #%end
 
-import sys, os
+import sys
+import os
 import shutil
 from grass.script import core as grass
 from grass.script import db as grassdb
-
+from types import DictType, ListType
 import json
 
-import pdb
 
-def returnClear(c,query):
+def returnClear(c, query):
     """Funtion to return a list of value from a query"""
     c.execute(query)
     bad = c.fetchall()
@@ -67,20 +67,41 @@
         good.append(b[0])
     return good
 
-def returnAll(c,query):
+
+def returnAll(c, query):
     """Function to return all the values from a query"""
     c.execute(query)
     return c.fetchall()
 
-def checkEle(c,table):
+
+def checkEle(c, table):
     """Function to return the number of elements in a table"""
     c.execute("select count(_id) from %s" % table)
     return c.fetchone()[0]
 
+
+def checkForm(jsonn):
+    """Function to check the forms"""
+    try:
+        form = jsonn['form']
+    except:
+        try:
+            form = jsonn['forms']
+        except:
+            grass.fatal(_("Error importing notes"))
+    if type(form) == DictType:
+        values = form['formitems']
+    elif type(form) == ListType and len(form) == 1:
+        values = form[0]['formitems']
+    elif type(form) == ListType and len(form) > 1:
+        grass.warning(_("Form contains more fields and it is no yet supported"))
+    return values
+
+
 def returnFormKeys(attr):
     """Function to return a string with the columns' name and type of form"""
-    js=json.loads(attr[0][3])
-    values=js['form']['formitems']
+    js = json.loads(attr[0][3])
+    values = checkForm(js)
     res = ''
     for v in values:
         if  v['type'].lower() == 'double':
@@ -94,14 +115,16 @@
         res += ", %s %s" % (v['key'].replace(' ', '_'), typ)
     return res
 
+
 def returnFormValues(attr):
     """Function to return a string with the values of form"""
-    js=json.loads(attr)
-    values=js['form']['formitems']
+    js = json.loads(attr)
+    values = checkForm(js)
     return','.join("'%s'" % v['value'] for v in values)
 
-def importGeom(vname, typ, c, owrite, z, cat = None):
-    psel = "SELECT lat, lon"
+
+def importGeom(vname, typ, c, owrite, z, cat=None):
+    psel = "SELECT lon, lat"
     if z == 'z':
         psel += ", altim"
         zcol = 3
@@ -113,12 +136,13 @@
     points = returnAll(c, psel)
     wpoi = '\n'.join(['|'.join([str(col) for col in row]) for row in points])
     # import points using v.in.ascii
-    if grass.write_command('v.in.ascii', flags = 't%s' % z, input='-', 
-                        output = vname, stdin = wpoi, z = zcol,
-                        overwrite = owrite, quiet = True) != 0:
-        grass.fatal(_("Error importing %s" % vname)) 
-    return points 
+    if grass.write_command('v.in.ascii', flags='t%s' % z, input='-', z=zcol,
+                           output=vname, stdin=wpoi, overwrite=owrite,
+                           quiet=True) != 0:
+        grass.fatal(_("Error importing %s" % vname))
+    return points
 
+
 def main():
     indb = options['database']
     prefix = options['basename']
@@ -142,40 +166,41 @@
     if not locn:
         # create new location and move to it creating new gisrc file
         new_loc = grass.basename(grass.tempfile(create=False))
-        grass.create_location(dbase = env['GISDBASE'],
-                              location = 'geopaparazzi_%s' % new_loc,
-                              epsg = '4326',
-                              desc = 'Temporary location for v.in.geopaparazzi')
+        new_loc_name = 'geopaparazzi_%s' % new_loc
+        grass.create_location(dbase=env['GISDBASE'], epsg='4326',
+                              location=new_loc_name,
+                              desc='Temporary location for v.in.geopaparazzi')
         grc = os.getenv('GISRC')
-        shutil.copyfile(grc,grc+'.old')
-        newrc = open(grc,'w')
+        shutil.copyfile(grc, grc + '.old')
+        newrc = open(grc, 'w')
         newrc.write('GISDBASE: %s\n' % env['GISDBASE'])
-        newrc.write('LOCATION_NAME: geopaparazzi_%s\n' % new_loc)
+        newrc.write('LOCATION_NAME: %s\n' % new_loc_name)
         newrc.write('MAPSET: PERMANENT\n')
         newrc.write('GRASS_GUI: text\n')
         newrc.close()
+        grass.run_command('db.connect', flags="d", quiet=True)
 
     # load bookmarks
     if flags['b']:
         # check if elements in bookmarks table are more the 0
         if checkEle(curs, 'bookmarks') != 0:
-            bookname = prefix + '_book'               
+            bookname = prefix + '_book'
             pois = importGeom(bookname, 'bookmarks', curs, owrite, '')
             sql = 'CREATE TABLE %s (cat int, text text)' % bookname
-            grass.write_command('db.execute', input='-', stdin = sql)
+            grass.write_command('db.execute', input='-', stdin=sql)
             # select attributes
             sql = "select text from bookmarks order by _id"
-            allattri = returnClear(curs,sql)
+            allattri = returnClear(curs, sql)
             # add values using insert statement
             idcat = 1
             for row in allattri:
-                values = "%d,'%s'" % (idcat,str(row))
+                values = "%d,'%s'" % (idcat, str(row))
                 sql = "insert into %s values(%s)" % (bookname, values)
-                grass.write_command('db.execute', input='-', stdin = sql)
+                grass.write_command('db.execute', input='-', stdin=sql)
                 idcat += 1
             # at the end connect table to vector
-            grass.run_command('v.db.connect', map = bookname, 
-                            table = bookname, quiet = True)
+            grass.run_command('v.db.connect', map=bookname,
+                              table=bookname, quiet=True)
         else:
             grass.warning(_("No bookmarks found, escape them"))
     # load images
@@ -186,21 +211,22 @@
             pois = importGeom(imagename, 'images', curs, owrite, d3)
             sql = 'CREATE TABLE %s (cat int, azim int, ' % imagename
             sql += 'path text, ts text, text text)'
-            grass.write_command('db.execute', input='-', stdin = sql)
+            grass.write_command('db.execute', input='-', stdin=sql)
             # select attributes
             sql = "select azim, path, ts, text from images order by _id"
-            allattri = returnAll(curs,sql)
+            allattri = returnAll(curs, sql)
             # add values using insert statement
             idcat = 1
             for row in allattri:
-                values = "%d,'%d','%s','%s','%s'" % (idcat,row[0],
-                str(row[1]), str(row[2]), str(row[3]))
+                values = "%d,'%d','%s','%s','%s'" % (idcat, row[0],
+                                                     str(row[1]), str(row[2]),
+                                                     str(row[3]))
                 sql = "insert into %s values(%s)" % (imagename, values)
-                grass.write_command('db.execute', input='-', stdin = sql)
+                grass.write_command('db.execute', input='-', stdin=sql)
                 idcat += 1
             # at the end connect table to vector
-            grass.run_command('v.db.connect', map = imagename, 
-                            table = imagename, quiet = True)
+            grass.run_command('v.db.connect', map=imagename, table=imagename,
+                              quiet=True)
         else:
             grass.warning(_("No images found, escape them"))
     # if tracks or nodes should be imported create a connection with sqlite3
@@ -209,66 +235,68 @@
         # check if elements in notes table are more the 0
         if checkEle(curs, 'notes') != 0:
             # select each categories
-            categories = returnClear(curs,"select cat from notes group by cat")
-            # for each category 
+            categories = returnClear(curs, "select cat from notes group by cat")
+            # for each category
             for cat in categories:
-                # select lat, lon for create point layer 
-                catname = prefix + '_notes_' + cat                
-                pois = importGeom(catname, 'notes', curs, owrite, d3, cat )
-                # select form to understand the number 
-                forms = returnClear(curs,"select _id from notes where cat = '%s' " \
-                            "and form is not null order by _id" % cat)
+                # select lat, lon for create point layer
+                catname = prefix + '_notes_' + cat
+                pois = importGeom(catname, 'notes', curs, owrite, d3, cat)
+                # select form to understand the number
+                forms = returnClear(curs, "select _id from notes where cat = '%s' " \
+                                    "and form is not null order by _id" % cat)
                 # if number of form is different from 0 and number of point
                 # remove the vector because some form it is different
                 if len(forms) != 0 and len(forms) != len(pois):
-                    grass.run_command('g.remove', vect = catname, quiet = True)
-                    grass.warning(_("Vector %s not imported because number of " \
-                    "points and form is different"))
+                    grass.run_command('g.remove', vect=catname, quiet=True)
+                    grass.warning(_("Vector %s not imported because number" \
+                                    " of points and form is different"))
                 # if form it's 0 there is no form
                 elif len(forms) == 0:
                     # create table without form
                     sql = 'CREATE TABLE %s (cat int, ts text, ' % catname
                     sql += 'text text, geopap_cat text)'
-                    grass.write_command('db.execute', input='-', stdin = sql)
+                    grass.write_command('db.execute', input='-', stdin=sql)
                     # select attributes
                     sql = "select ts, text, cat from notes where "\
                         "cat='%s' order by _id" % cat
-                    allattri = returnAll(curs,sql)
+                    allattri = returnAll(curs, sql)
                     # add values using insert statement
                     idcat = 1
                     for row in allattri:
-                        values = "%d,'%s','%s','%s'" % (idcat,str(row[0]),
-                        str(row[1]), str(row[2]))
+                        values = "%d,'%s','%s','%s'" % (idcat, str(row[0]),
+                                                        str(row[1]),
+                                                        str(row[2]))
                         sql = "insert into %s values(%s)" % (catname, values)
-                        grass.write_command('db.execute', input='-', stdin = sql)
+                        grass.write_command('db.execute', input='-', stdin=sql)
                         idcat += 1
                     # at the end connect table to vector
-                    grass.run_command('v.db.connect', map = catname, 
-                                    table = catname, quiet = True)
+                    grass.run_command('v.db.connect', map=catname, 
+                                      table=catname, quiet=True)
                 # create table with form
                 else:
                     # select all the attribute
                     sql = "select ts, text, cat, form from notes where "\
-                        "cat='%s' order by _id" % cat
-                    allattri = returnAll(curs,sql)
+                          "cat='%s' order by _id" % cat
+                    allattri = returnAll(curs, sql)
                     # return string of form's categories too create table
                     keys = returnFormKeys(allattri)
                     sql = 'CREATE TABLE %s (cat int, ts text, ' % catname
                     sql += 'text text, geopap_cat text %s)' % keys
-                    grass.write_command('db.execute', input='-', stdin = sql)
+                    grass.write_command('db.execute', input='-', stdin=sql)
                     # it's for the number of categories
                     idcat = 1
-                    # for each feature insert value                    
+                    # for each feature insert value
                     for row in allattri:
-                        values = "%d,'%s','%s','%s'," % (idcat,str(row[0]),
-                        str(row[1]), str(row[2]))
+                        values = "%d,'%s','%s','%s'," % (idcat, str(row[0]),
+                                                         str(row[1]),
+                                                         str(row[2]))
                         values += returnFormValues(row[3])
                         sql = "insert into %s values(%s)" % (catname, values)
-                        grass.write_command('db.execute', input='-', stdin = sql)
+                        grass.write_command('db.execute', input='-', stdin=sql)
                         idcat += 1
                     # at the end connect table with vector
-                    grass.run_command('v.db.connect', map = catname, 
-                                    table = catname, quiet = True)
+                    grass.run_command('v.db.connect', map=catname,
+                                      table=catname, quiet=True)
         else:
             grass.warning(_("No notes found, escape them"))
     # load tracks
@@ -279,7 +307,7 @@
             # define string for insert data at the end
             tracks = ''
             # return ids of tracks
-            ids = returnClear(curs,"select _id from gpslogs")
+            ids = returnClear(curs, "select _id from gpslogs")
             # for each track
             for i in ids:
                 # select all the points coordinates
@@ -287,7 +315,7 @@
                 if flags['z']:
                     tsel += ", altim"
                 tsel += " from gpslog_data where logid=%s order by _id" % i
-                trackpoints = returnAll(curs,tsel)
+                trackpoints = returnAll(curs, tsel)
                 wpoi = '\n'.join(['|'.join([str(col) for col in row]) for row in trackpoints])
                 tracks += "%s\n" % wpoi
                 if flags['z']:
@@ -295,55 +323,58 @@
                 else:
                     tracks += 'NaN|Nan\n'
             # import lines
-            if grass.write_command('v.in.lines', flags = d3, input = '-', 
-                                out = tracksname, stdin = tracks,
-                                overwrite = owrite, quiet = True) != 0:
+            if grass.write_command('v.in.lines', flags=d3, input='-',
+                                   out=tracksname, stdin=tracks,
+                                   overwrite=owrite, quiet=True) != 0:
                 grass.fatal(_("Error importing %s" % tracksname))
             # create table for line
-            sql = 'CREATE TABLE %s (cat int, startts text, endts text, ' % tracksname
-            sql += ' text text, color text, width int)'
-            grass.write_command('db.execute', input='-', stdin = sql)
-            sql = "select logid, startts, endts, text, color, width from gpslogs, "\
-                "gpslogsproperties where gpslogs._id=gpslogsproperties.logid"
+            sql='CREATE TABLE %s (cat int, startts text, ' % tracksname
+            sql += 'endts text, text text, color text, width int)'
+            grass.write_command('db.execute', input='-', stdin=sql)
+            sql = "select logid, startts, endts, text, color, width from" \
+                  " gpslogs, gpslogsproperties where gpslogs._id=" \
+                  "gpslogsproperties.logid"
             # return attributes
-            allattri = returnAll(curs,sql)
+            allattri = returnAll(curs, sql)
             # for each line insert attribute
             for row in allattri:
-                values = "%d,'%s','%s','%s','%s',%d" % (row[0],str(row[1]),
-                            str(row[2]), str(row[3]), str(row[4]), row[5])
+                values = "%d,'%s','%s','%s','%s',%d" % (row[0], str(row[1]),
+                                                        str(row[2]),
+                                                        str(row[3]),
+                                                        str(row[4]), row[5])
                 sql = "insert into %s values(%s)" % (tracksname, values)
-                grass.write_command('db.execute', input='-', stdin = sql)
+                grass.write_command('db.execute', input='-', stdin=sql)
             # at the end connect map with table
-            grass.run_command('v.db.connect', map = tracksname, 
-                            table = tracksname, quiet = True)   
+            grass.run_command('v.db.connect', map=tracksname,
+                              table=tracksname, quiet=True)
         else:
             grass.warning(_("No tracks found, escape them"))
     # if location it's not latlong reproject it
     if not locn:
         # copy restore the original location
-        shutil.copyfile(grc+'.old',grc)
+        shutil.copyfile(grc + '.old', grc)
         # reproject bookmarks
         if flags['b'] and checkEle(curs, 'bookmarks') != 0:
-            grass.run_command('v.proj', quiet = True, input = bookname, 
-                            location = 'geopaparazzi_%s' % new_loc, 
-                            mapset = 'PERMANENT')
+            grass.run_command('v.proj', quiet=True, input=bookname,
+                              location='geopaparazzi_%s' % new_loc,
+                              mapset='PERMANENT')
         # reproject images
         if flags['i'] and checkEle(curs, 'images') != 0:
-            grass.run_command('v.proj', quiet = True, input = imagename, 
-                            location = 'geopaparazzi_%s' % new_loc, 
-                            mapset = 'PERMANENT')   
+            grass.run_command('v.proj', quiet=True, input=imagename,
+                              location='geopaparazzi_%s' % new_loc,
+                              mapset='PERMANENT')
         # reproject notes
         if flags['n'] and checkEle(curs, 'notes') != 0:
             for cat in categories:
                 catname = prefix + '_node_' + cat
-                grass.run_command('v.proj', quiet = True, input = catname, 
-                            location = 'geopaparazzi_%s' % new_loc, 
-                            mapset = 'PERMANENT')
+                grass.run_command('v.proj', quiet=True, input=catname,
+                                  location='geopaparazzi_%s' % new_loc,
+                                  mapset='PERMANENT')
         # reproject track
         if flags['t'] and checkEle(curs, 'gpslogs') != 0:
-             grass.run_command('v.proj', quiet = True, input = tracksname, 
-                            location = 'geopaparazzi_%s' % new_loc, 
-                            mapset = 'PERMANENT')              
+             grass.run_command('v.proj', quiet=True, input=tracksname,
+                               location='geopaparazzi_%s' % new_loc,
+                               mapset='PERMANENT')
 
 if __name__ == "__main__":
     options, flags = grass.parser()



More information about the grass-commit mailing list