v.in.gns.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.in.gns
  5. #
  6. # AUTHOR(S): Markus Neteler, neteler itc it
  7. # Converted to Python by Glynn Clements
  8. #
  9. # PURPOSE: Import GEOnet Names Server (GNS) country files into a GRASS vector map
  10. # http://earth-info.nga.mil/gns/html/
  11. # -> Download Names Files for Countries and Territories (FTP)
  12. #
  13. # Column names: http://earth-info.nga.mil/gns/html/help.htm
  14. #
  15. # COPYRIGHT: (c) 2005 GRASS Development Team
  16. #
  17. # This program is free software under the GNU General Public
  18. # License (>=v2). Read the file COPYING that comes with GRASS
  19. # for details.
  20. #
  21. # TODO: - see below in the code
  22. # - add extra columns explaining some column acronyms,
  23. # e.g. FC (Feature Classification)
  24. #############################################################################
  25. #%Module
  26. #% description: Imports US-NGA GEOnet Names Server (GNS) country files into a GRASS vector points map.
  27. #% keywords: vector
  28. #% keywords: import
  29. #% keywords: gazetteer
  30. #%End
  31. #%option
  32. #% key: file
  33. #% type: string
  34. #% description: Uncompressed GNS file from NGA (with .txt extension)
  35. #% gisprompt: old_file,file,input
  36. #% required : yes
  37. #%end
  38. #%option
  39. #% key: vect
  40. #% type: string
  41. #% gisprompt: new,vector,vector
  42. #% description: Name for output vector map
  43. #% required : no
  44. #%end
  45. import sys
  46. import os
  47. from grass.script import core as grass
  48. def main():
  49. fileorig = options['file']
  50. filevect = options['vect']
  51. if not filevect:
  52. filevect = grass.basename(fileorig, 'txt')
  53. #are we in LatLong location?
  54. s = grass.read_command("g.proj", flags='j')
  55. kv = grass.parse_key_val(s)
  56. if kv['+proj'] != 'longlat':
  57. grass.fatal(_("This module only operates in LatLong/WGS84 locations"))
  58. #### setup temporary file
  59. tmpfile = grass.tempfile()
  60. coldescs = [("RC", "rc integer"),
  61. ("UFI", "uf1 integer"),
  62. ("UNI", "uni integer"),
  63. ("LAT", "lat double precision"),
  64. ("LONG", "lon double precision"),
  65. ("DMS_LAT", "dms_lat varchar(6)"),
  66. ("DMS_LONG", "dms_long varchar(7)"),
  67. ("UTM", "utm varchar(4)"),
  68. ("JOG", "jog varchar(7)"),
  69. ("FC", "fc varchar(1)"),
  70. ("DSG", "dsg varchar(5)"),
  71. ("PC", "pc integer"),
  72. ("CC1", "cci varchar(2)"),
  73. ("ADM1", "adm1 varchar(2)"),
  74. ("ADM2", "adm2 varchar(200)"),
  75. ("DIM", "dim integer"),
  76. ("CC2", "cc2 varchar(2)"),
  77. ("NT", "nt varchar(1)"),
  78. ("LC", "lc varchar(3)"),
  79. ("SHORT_FORM", "shortform varchar(128)"),
  80. ("GENERIC", "generic varchar(128)"),
  81. ("SORT_NAME", "sortname varchar(200)"),
  82. ("FULL_NAME", "fullname varchar(200)"),
  83. ("FULL_NAME_ND","funamesd varchar(200)"),
  84. ("MODIFY_DATE", "mod_date date")]
  85. colnames = [desc[0] for desc in coldescs]
  86. coltypes = dict([(desc[0], 'integer' in desc[1]) for desc in coldescs])
  87. header = None
  88. num_places = 0
  89. inf = file(fileorig)
  90. outf = file(tmpfile, 'wb')
  91. for line in inf:
  92. fields = line.rstrip('\r\n').split('\t')
  93. if not header:
  94. header = fields
  95. continue
  96. vars = dict(zip(header, fields))
  97. fields2 = []
  98. for col in colnames:
  99. if col in vars:
  100. if coltypes[col] and vars[col] == '':
  101. fields2.append('0')
  102. else:
  103. fields2.append(vars[col])
  104. else:
  105. if coltypes[col]:
  106. fields2.append('0')
  107. else:
  108. fields2.append('')
  109. line2 = ';'.join(fields2) + '\n'
  110. outf.write(line2)
  111. num_places += 1
  112. outf.close()
  113. inf.close()
  114. grass.message(_("Converted %d place names.") % num_places)
  115. #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS
  116. # Solution:
  117. # IN=DDMMSS
  118. # DEG=`echo $IN | cut -b1,2`
  119. # MIN=`echo $IN | cut -b3,4`
  120. # SEC=`echo $IN | cut -b5,6`
  121. # DEG_STR="$DEG:$MIN:$SEC"
  122. #modifications (to match DBF 10 char column name limit):
  123. # short_form -> shortform
  124. # sort_name -> sortname
  125. # full_name -> fullname
  126. # full_name_sd -> funamesd
  127. # pump data into GRASS:
  128. columns = [desc[1] for desc in coldescs]
  129. grass.run_command('v.in.ascii', cat = 0, x = 5, y = 4, fs = ';',
  130. input = tmpfile, output = filevect,
  131. columns = columns)
  132. grass.try_remove(tmpfile)
  133. # write cmd history:
  134. grass.vector_history(filevect)
  135. if __name__ == "__main__":
  136. options, flags = grass.parser()
  137. main()