[GRASS-SVN] r33525 - in grass/trunk/scripts: v.in.geonames v.in.gns

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Sep 24 14:48:06 EDT 2008


Author: glynn
Date: 2008-09-24 14:48:06 -0400 (Wed, 24 Sep 2008)
New Revision: 33525

Added:
   grass/trunk/scripts/v.in.geonames/v.in.geonames.py
   grass/trunk/scripts/v.in.gns/v.in.gns.py
Log:
Convert v.in.gns, v.in.geonames to Python


Added: grass/trunk/scripts/v.in.geonames/v.in.geonames.py
===================================================================
--- grass/trunk/scripts/v.in.geonames/v.in.geonames.py	                        (rev 0)
+++ grass/trunk/scripts/v.in.geonames/v.in.geonames.py	2008-09-24 18:48:06 UTC (rev 33525)
@@ -0,0 +1,160 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.in.geonames
+#
+# AUTHOR(S):    Markus Neteler, neteler cealp it
+#               Converted to Python by Glynn Clements
+#
+# PURPOSE:      Import geonames.org dumps
+#               http://download.geonames.org/export/dump/
+#
+#               Feature Codes: http://www.geonames.org/export/codes.html
+#
+# COPYRIGHT:    (c) 2008 Markus Neteler, 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.
+#
+# TODO: fix encoding issues for Asian fonts in 'alternatename' column (v.in.ascii)
+#       fix spurious char stuff in elevation column
+#############################################################################
+
+#%Module
+#%  description: Imports geonames.org country files into a GRASS vector points map.
+#%  keywords: vector, import, gazetteer
+#%End
+#%option
+#% key: input
+#% type: string
+#% key_desc: name
+#% description: Uncompressed geonames file from (with .txt extension)
+#% gisprompt: old_file,file,input
+#% required : yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% key_desc: name
+#% gisprompt: new,vector,vector
+#% description: Name for output vector map
+#% required : yes
+#%end
+
+import sys
+import os
+import grass
+
+def main():
+    infile  = options['input']
+    outfile = options['output']
+    
+    # save command line
+    cmdline = os.path.basename(sys.argv[0])
+    if infile:
+	cmdline += ' input=' + infile
+    if outfile:
+	cmdline += ' output=' + outfile
+
+    #### setup temporary file
+    tmpfile = grass.tempfile()
+
+    #are we in LatLong location?
+    s = grass.read_command("g.proj", flags='j')
+    kv = grass.parse_key_val(s)
+    if kv['+proj'] != 'longlat':
+	grass.fatal("This module only operates in LatLong/WGS84 locations")
+
+    # input test
+    if not os.access(infile, os.R_OK):
+	grass.fatal("File <%s> not found" % infile)
+
+    # DBF doesn't support lengthy text fields
+    s = grass.read_command('db.connect', flags = 'p')
+    kv = grass.parse_key_val(s, ':')
+    dbfdriver = kv['driver'] == 'dbf'
+    if dbfdriver:
+	grass.warning("Since DBF driver is used, the content of the 'alternatenames' column might be cut with respect to the original Geonames.org column content")
+
+    #let's go
+    #change TAB to vertical bar
+    num_places = 0
+    inf = file(infile)
+    outf = file(tmpfile, 'wb')
+    for line in inf:
+	fields = line.rstrip('\r\n').split('\t')
+	line2 = '|'.join(fields) + '\n'
+	outf.write(line2)
+	num_places += 1
+    outf.close()
+    inf.close()
+
+    grass.message("Converted %d place names." % num_places)
+
+    # pump data into GRASS:
+    #  http://download.geonames.org/export/dump/readme.txt
+    #  The main 'geoname' table has the following fields :
+    #  ---------------------------------------------------
+    #  geonameid         : integer id of record in geonames database
+    #  name              : name of geographical point (utf8) varchar(200)
+    #  asciiname         : name of geographical point in plain ascii characters, varchar(200)
+    #  alternatenames    : alternatenames, comma separated varchar(4000)
+    #  latitude          : latitude in decimal degrees (wgs84)
+    #  longitude         : longitude in decimal degrees (wgs84)
+    #  feature class     : see http://www.geonames.org/export/codes.html, char(1)
+    #  feature code      : see http://www.geonames.org/export/codes.html, varchar(10)
+    #  country code      : ISO-3166 2-letter country code, 2 characters
+    #  cc2               : alternate country codes, comma separated, ISO-3166 2-letter country code, 60 characters
+    #  admin1 code       : fipscode (subject to change to iso code), isocode for the us and ch, see file admin1Codes.txt for display names of this code; varchar(20)
+    #  admin2 code       : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
+    #  admin3 code       : code for third level administrative division, varchar(20)
+    #  admin4 code       : code for fourth level administrative division, varchar(20)
+    #  population        : integer
+    #  elevation         : in meters, integer
+    #  gtopo30           : average elevation of 30'x30' (ca 900mx900m) area in meters, integer
+    #  timezone          : the timezone id (see file http://download.geonames.org/export/dump/timeZones.txt)
+    #  modification date : date of last modification in yyyy-MM-dd format
+
+    # geonameid|name|asciiname|alternatenames|latitude|longitude|featureclass|featurecode|countrycode|cc2|admin1code|admin2code|admin3code|admin4code|population|elevation|gtopo30|timezone|modificationdate
+
+    # TODO: elevation seems to contain spurious char stuff :(
+
+    # debug:
+    # head -n 3 ${TMPFILE}.csv
+
+    columns = ['geonameid integer',
+	       'name varchar(200)',
+	       'asciiname varchar(200)',
+	       'alternatename varchar(4000)',
+	       'latitude double precision',
+	       'longitude double precision',
+	       'featureclass varchar(1)',
+	       'featurecode varchar(10)',
+	       'countrycode varchar(2)',
+	       'cc2 varchar(60)',
+	       'admin1code varchar(20)',
+	       'admin2code varchar(20)',
+	       'admin3code varchar(20)',
+	       'admin4code varchar(20)',
+	       'population integer',
+	       'elevation varchar(5)',
+	       'gtopo30 integer',
+	       'timezone varchar(50)',
+	       'modification date']
+
+    grass.run_command('v.in.ascii', cat = 0, x = 6, y = 5, fs = '|',
+		      input = tmpfile, output = outfile,
+		      columns = columns)
+
+    os.remove(tmpfile)
+
+    # write cmd history:
+    grass.run_command('v.support', map = outfile, cmdhist = cmdline)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    main()
+


Property changes on: grass/trunk/scripts/v.in.geonames/v.in.geonames.py
___________________________________________________________________
Name: svn:executable
   + *

Added: grass/trunk/scripts/v.in.gns/v.in.gns.py
===================================================================
--- grass/trunk/scripts/v.in.gns/v.in.gns.py	                        (rev 0)
+++ grass/trunk/scripts/v.in.gns/v.in.gns.py	2008-09-24 18:48:06 UTC (rev 33525)
@@ -0,0 +1,165 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.in.gns
+#
+# AUTHOR(S):    Markus Neteler, neteler itc it
+#               Converted to Python by Glynn Clements
+#
+# PURPOSE:      Import GEOnet Names Server (GNS) country files into a GRASS vector map
+#               http://earth-info.nga.mil/gns/html/
+#                -> Download Names Files for Countries and Territories (FTP)
+#
+#               Column names: http://earth-info.nga.mil/gns/html/help.htm
+#
+# COPYRIGHT:    (c) 2005 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.
+#
+# TODO:         - see below in the code
+#               - add extra columns explaining some column acronyms, 
+#                 e.g. FC (Feature Classification)
+#############################################################################
+
+#%Module
+#%  description: Imports US-NGA GEOnet Names Server (GNS) country files into a GRASS vector points map.
+#%  keywords: vector, import, gazetteer
+#%End
+#%option
+#% key: file
+#% type: string
+#% description: Uncompressed GNS file from NGA (with .txt extension)
+#% gisprompt: old_file,file,input
+#% required : yes
+#%end
+#%option
+#% key: vect
+#% type: string
+#% gisprompt: new,vector,vector
+#% description: Name for output vector map
+#% required : no
+#%end
+
+import sys
+import os
+import grass
+
+def main():
+    fileorig = options['file']
+    filevect = options['vect']
+    
+    # save command line
+    cmdline = os.path.basename(sys.argv[0])
+    if fileorig:
+	cmdline += ' file=' + fileorig
+    if filevect:
+	cmdline += ' vect=' + filevect
+
+    if not filevect:
+	filevect = os.path.basename(fileorig)
+	fs = os.path.splitext(filevect)
+	if fs[1].lower() == '.txt':
+	    filevect = fs[0]
+
+    #are we in LatLong location?
+    s = grass.read_command("g.proj", flags='j')
+    kv = grass.parse_key_val(s)
+    if kv['+proj'] != 'longlat':
+	grass.fatal("This module only operates in LatLong/WGS84 locations")
+
+    #### setup temporary file
+    tmpfile = grass.tempfile()
+
+    coldescs = [("RC",		"rc integer"),
+		("UFI",		"uf1 integer"),
+		("UNI",		"uni integer"),
+		("LAT",		"lat double precision"),
+		("LONG",	"lon double precision"),
+		("DMS_LAT",	"dms_lat varchar(6)"),
+		("DMS_LONG",	"dms_long varchar(7)"),
+		("UTM",		"utm varchar(4)"),
+		("JOG",		"jog varchar(7)"),
+		("FC",		"fc varchar(1)"),
+		("DSG",		"dsg varchar(5)"),
+		("PC",		"pc integer"),
+		("CC1",		"cci varchar(2)"),
+		("ADM1",	"adm1 varchar(2)"),
+		("ADM2",	"adm2 varchar(200)"),
+		("DIM",		"dim integer"),
+		("CC2",		"cc2 varchar(2)"),
+		("NT",		"nt varchar(1)"),
+		("LC",		"lc varchar(3)"),
+		("SHORT_FORM",	"shortform varchar(128)"),
+		("GENERIC",	"generic varchar(128)"),
+		("SORT_NAME",	"sortname varchar(200)"),
+		("FULL_NAME",	"fullname varchar(200)"),
+		("FULL_NAME_ND","funamesd varchar(200)"),
+		("MODIFY_DATE",	"mod_date date")]
+
+    colnames = [desc[0] for desc in coldescs]
+    coltypes = dict([(desc[0], 'integer' in desc[1]) for desc in coldescs])
+
+    header = None
+    num_places = 0
+    inf = file(fileorig)
+    outf = file(tmpfile, 'wb')
+    for line in inf:
+	fields = line.rstrip('\r\n').split('\t')
+	if not header:
+	    header = fields
+	    continue
+	vars = dict(zip(header, fields))
+	fields2 = []
+	for col in colnames:
+	    if col in vars:
+		if coltypes[col] and vars[col] == '':
+		    fields2.append('0')
+		else:
+		    fields2.append(vars[col])
+	    else:
+		if coltypes[col]:
+		    fields2.append('0')
+		else:
+		    fields2.append('')
+	line2 = ';'.join(fields2) + '\n'
+	outf.write(line2)
+	num_places += 1
+    outf.close()
+    inf.close()
+
+    grass.message("Converted %d place names." % num_places)
+
+    #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS
+    # Solution:
+    # IN=DDMMSS
+    # DEG=`echo $IN | cut -b1,2`
+    # MIN=`echo $IN | cut -b3,4`
+    # SEC=`echo $IN | cut -b5,6`
+    # DEG_STR="$DEG:$MIN:$SEC"
+    
+    #modifications (to match DBF 10 char column name limit):
+    # short_form   -> shortform
+    # sort_name    -> sortname
+    # full_name    -> fullname
+    # full_name_sd -> funamesd
+
+    # pump data into GRASS:
+
+    columns = [desc[1] for desc in coldescs]
+
+    grass.run_command('v.in.ascii', cat = 0, x = 5, y = 4, fs = ';',
+		      input = tmpfile, output = filevect,
+		      columns = columns)
+
+    os.remove(tmpfile)
+
+    # write cmd history:
+    grass.run_command('v.support', map = filevect, cmdhist = cmdline)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()
+


Property changes on: grass/trunk/scripts/v.in.gns/v.in.gns.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list