v.in.gns.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, import, gazetteer
  28. #%End
  29. #%option
  30. #% key: file
  31. #% type: string
  32. #% description: Uncompressed GNS file from NGA (with .txt extension)
  33. #% gisprompt: old_file,file,input
  34. #% required : yes
  35. #%end
  36. #%option
  37. #% key: vect
  38. #% type: string
  39. #% gisprompt: new,vector,vector
  40. #% description: Name for output vector map
  41. #% required : no
  42. #%end
  43. import sys
  44. import os
  45. import grass
  46. def main():
  47. fileorig = options['file']
  48. filevect = options['vect']
  49. if not filevect:
  50. filevect = grass.basename(fileorig, 'txt')
  51. #are we in LatLong location?
  52. s = grass.read_command("g.proj", flags='j')
  53. kv = grass.parse_key_val(s)
  54. if kv['+proj'] != 'longlat':
  55. grass.fatal("This module only operates in LatLong/WGS84 locations")
  56. #### setup temporary file
  57. tmpfile = grass.tempfile()
  58. coldescs = [("RC", "rc integer"),
  59. ("UFI", "uf1 integer"),
  60. ("UNI", "uni integer"),
  61. ("LAT", "lat double precision"),
  62. ("LONG", "lon double precision"),
  63. ("DMS_LAT", "dms_lat varchar(6)"),
  64. ("DMS_LONG", "dms_long varchar(7)"),
  65. ("UTM", "utm varchar(4)"),
  66. ("JOG", "jog varchar(7)"),
  67. ("FC", "fc varchar(1)"),
  68. ("DSG", "dsg varchar(5)"),
  69. ("PC", "pc integer"),
  70. ("CC1", "cci varchar(2)"),
  71. ("ADM1", "adm1 varchar(2)"),
  72. ("ADM2", "adm2 varchar(200)"),
  73. ("DIM", "dim integer"),
  74. ("CC2", "cc2 varchar(2)"),
  75. ("NT", "nt varchar(1)"),
  76. ("LC", "lc varchar(3)"),
  77. ("SHORT_FORM", "shortform varchar(128)"),
  78. ("GENERIC", "generic varchar(128)"),
  79. ("SORT_NAME", "sortname varchar(200)"),
  80. ("FULL_NAME", "fullname varchar(200)"),
  81. ("FULL_NAME_ND","funamesd varchar(200)"),
  82. ("MODIFY_DATE", "mod_date date")]
  83. colnames = [desc[0] for desc in coldescs]
  84. coltypes = dict([(desc[0], 'integer' in desc[1]) for desc in coldescs])
  85. header = None
  86. num_places = 0
  87. inf = file(fileorig)
  88. outf = file(tmpfile, 'wb')
  89. for line in inf:
  90. fields = line.rstrip('\r\n').split('\t')
  91. if not header:
  92. header = fields
  93. continue
  94. vars = dict(zip(header, fields))
  95. fields2 = []
  96. for col in colnames:
  97. if col in vars:
  98. if coltypes[col] and vars[col] == '':
  99. fields2.append('0')
  100. else:
  101. fields2.append(vars[col])
  102. else:
  103. if coltypes[col]:
  104. fields2.append('0')
  105. else:
  106. fields2.append('')
  107. line2 = ';'.join(fields2) + '\n'
  108. outf.write(line2)
  109. num_places += 1
  110. outf.close()
  111. inf.close()
  112. grass.message("Converted %d place names." % num_places)
  113. #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS
  114. # Solution:
  115. # IN=DDMMSS
  116. # DEG=`echo $IN | cut -b1,2`
  117. # MIN=`echo $IN | cut -b3,4`
  118. # SEC=`echo $IN | cut -b5,6`
  119. # DEG_STR="$DEG:$MIN:$SEC"
  120. #modifications (to match DBF 10 char column name limit):
  121. # short_form -> shortform
  122. # sort_name -> sortname
  123. # full_name -> fullname
  124. # full_name_sd -> funamesd
  125. # pump data into GRASS:
  126. columns = [desc[1] for desc in coldescs]
  127. grass.run_command('v.in.ascii', cat = 0, x = 5, y = 4, fs = ';',
  128. input = tmpfile, output = filevect,
  129. columns = columns)
  130. grass.try_remove(tmpfile)
  131. # write cmd history:
  132. grass.vector_history(filevect)
  133. if __name__ == "__main__":
  134. options, flags = grass.parser()
  135. main()