[GRASS-SVN] r62271 - grass/trunk/scripts/v.in.geonames
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Oct 16 18:14:04 PDT 2014
Author: neteler
Date: 2014-10-16 18:14:03 -0700 (Thu, 16 Oct 2014)
New Revision: 62271
Modified:
grass/trunk/scripts/v.in.geonames/v.in.geonames.py
Log:
v.in.geonames: fix indentation; save elevation as integer; use tab as separator
Modified: grass/trunk/scripts/v.in.geonames/v.in.geonames.py
===================================================================
--- grass/trunk/scripts/v.in.geonames/v.in.geonames.py 2014-10-16 22:32:19 UTC (rev 62270)
+++ grass/trunk/scripts/v.in.geonames/v.in.geonames.py 2014-10-17 01:14:03 UTC (rev 62271)
@@ -12,7 +12,7 @@
#
# Feature Codes: http://www.geonames.org/export/codes.html
#
-# COPYRIGHT: (c) 2008 Markus Neteler, GRASS Development Team
+# COPYRIGHT: (c) 2008-2014 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
@@ -34,7 +34,6 @@
#%option G_OPT_V_OUTPUT
#%end
-import sys
import os
import grass.script as grass
@@ -50,31 +49,20 @@
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"))
+ 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)
+ grass.fatal(_("File <%s> not found") % infile)
# DBF doesn't support lengthy text fields
kv = grass.db_connection()
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"))
+ 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()
-
+ with open(infile) as f:
+ num_places = sum(1 for _ in f)
grass.message(_("Converting %d place names...") % num_places)
# pump data into GRASS:
@@ -109,49 +97,49 @@
# use different column names limited to 10 chars for dbf
if dbfdriver:
- columns = ['geonameid integer',
- 'name varchar(200)',
- 'asciiname varchar(200)',
- 'altname varchar(4000)',
- 'latitude double precision',
- 'longitude double precision',
- 'featrclass varchar(1)',
- 'featrcode varchar(10)',
- 'cntrycode 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)',
- 'mod_date date']
+ columns = ['geonameid integer',
+ 'name varchar(200)',
+ 'asciiname varchar(200)',
+ 'altname varchar(4000)',
+ 'latitude double precision',
+ 'longitude double precision',
+ 'featrclass varchar(1)',
+ 'featrcode varchar(10)',
+ 'cntrycode varchar(2)',
+ 'cc2 varchar(60)',
+ 'admin1code varchar(20)',
+ 'admin2code varchar(20)',
+ 'admin3code varchar(20)',
+ 'admin4code varchar(20)',
+ 'population integer',
+ 'elevation integer',
+ 'gtopo30 integer',
+ 'timezone varchar(50)',
+ 'mod_date date']
else:
- 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']
+ 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 integer',
+ 'gtopo30 integer',
+ 'timezone varchar(50)',
+ 'modification date']
- grass.run_command('v.in.ascii', cat = 0, x = 6, y = 5, sep = '|',
- input = tmpfile, output = outfile,
- columns = columns)
+ grass.run_command('v.in.ascii', cat = 0, x = 6, y = 5, sep = 'tab',
+ input = infile, output = outfile,
+ columns = columns)
grass.try_remove(tmpfile)
More information about the grass-commit
mailing list