v.in.gns 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/sh
  2. ############################################################################
  3. #
  4. # MODULE: v.in.gns
  5. #
  6. # AUTHOR(S): Markus Neteler, neteler itc it
  7. #
  8. # PURPOSE: Import GEOnet Names Server (GNS) country files into a GRASS vector map
  9. # http://earth-info.nga.mil/gns/html/
  10. # -> Download Names Files for Countries and Territories (FTP)
  11. #
  12. # Column names: http://earth-info.nga.mil/gns/html/help.htm
  13. #
  14. # COPYRIGHT: (c) 2005 GRASS Development Team
  15. #
  16. # This program is free software under the GNU General Public
  17. # License (>=v2). Read the file COPYING that comes with GRASS
  18. # for details.
  19. #
  20. # TODO: - see below in the code
  21. # - add extra columns explaining some column acronyms,
  22. # e.g. FC (Feature Classification)
  23. #############################################################################
  24. #%Module
  25. #% description: Imports US-NGA GEOnet Names Server (GNS) country files into a GRASS vector points map.
  26. #% keywords: vector, import, gazetteer
  27. #%End
  28. #%option
  29. #% key: file
  30. #% type: string
  31. #% description: Uncompressed GNS file from NGA (with .txt extension)
  32. #% gisprompt: old_file,file,input
  33. #% required : yes
  34. #%end
  35. #%option
  36. #% key: vect
  37. #% type: string
  38. #% gisprompt: new,vector,vector
  39. #% description: Name for output vector map
  40. #% required : no
  41. #%end
  42. if [ -z "$GISBASE" ] ; then
  43. echo "You must be in GRASS GIS to run this program." >&2
  44. exit 1
  45. fi
  46. # save command line
  47. if [ "$1" != "@ARGS_PARSED@" ] ; then
  48. CMDLINE="`basename $0`"
  49. for arg in "$@" ; do
  50. CMDLINE="$CMDLINE \"$arg\""
  51. done
  52. export CMDLINE
  53. exec g.parser "$0" "$@"
  54. fi
  55. PROG=`basename $0`
  56. #### check if we have awk
  57. if [ ! -x "`which awk`" ] ; then
  58. g.message -e "awk required, please install awk/gawk first"
  59. exit 1
  60. fi
  61. # setting environment, so that awk works properly in all languages
  62. unset LC_ALL
  63. LC_NUMERIC=C
  64. export LC_NUMERIC
  65. eval `g.gisenv`
  66. : ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
  67. LOCATION="$GISDBASE"/"$LOCATION_NAME"/"$MAPSET"
  68. if [ -n "$GIS_OPT_FILE" ] ; then
  69. FILEORIG="$GIS_OPT_FILE"
  70. if [ -n "$GIS_OPT_VECT" ] ; then
  71. FILE="$GIS_OPT_VECT"
  72. else
  73. FILE=`basename $FILEORIG .txt`
  74. fi
  75. fi
  76. #### setup temporary file
  77. TMPFILE="`g.tempfile pid=$$`"
  78. if [ $? -ne 0 ] || [ -z "$TMPFILE" ] ; then
  79. g.message -e "Unable to create temporary files"
  80. exit 1
  81. fi
  82. #### trap ctrl-c so that we can clean up tmp
  83. trap 'rm -f "${TMPFILE}" "${TMPFILE}_ascii.csv"' 2 3 15
  84. #are we in LatLong location?
  85. g.proj -p | grep -i name | grep -i Lon > /dev/null
  86. if [ $? -eq 1 ] ; then
  87. g.message -e "This module only operates in LatLong/WGS84 locations"
  88. exit 1
  89. fi
  90. #let's go
  91. #change TAB to semicolon
  92. TAB=`awk 'BEGIN{printf "\t";}'`
  93. cat "${FILEORIG}" | sed "s+$TAB+;+g" > "${TMPFILE}"
  94. #header elimination:
  95. HEADER="RC;UFI;UNI;LAT;LONG;DMS_LAT;DMS_LONG;UTM;JOG;FC;DSG;PC;CC1;ADM1;ADM2;DIM;CC2;NT;LC;SHORT_FORM;GENERIC;SORT_NAME;FULL_NAME;FULL_NAME_ND;MODIFY_DATE"
  96. cat "${TMPFILE}" | grep -v "^${HEADER}" > "${TMPFILE}_ascii.csv"
  97. rm -f "${TMPFILE}"
  98. NUM_PLACES=`wc -l "${TMPFILE}_ascii.csv" | awk '{print $1}'`
  99. g.message "Converted $NUM_PLACES place names."
  100. #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS
  101. # Solution:
  102. # IN=DDMMSS
  103. # DEG=`echo $IN | cut -b1,2`
  104. # MIN=`echo $IN | cut -b3,4`
  105. # SEC=`echo $IN | cut -b5,6`
  106. # DEG_STR="$DEG:$MIN:$SEC"
  107. #modifications (to match DBF 10 char column name limit):
  108. # short_form -> shortform
  109. # sort_name -> sortname
  110. # full_name -> fullname
  111. # full_name_sd -> funamesd
  112. #
  113. # pump data into GRASS:
  114. v.in.ascii cat=0 x=5 y=4 fs=';' in="${TMPFILE}_ascii.csv" out="$FILE" \
  115. columns='rc integer,uf1 integer,uni integer,lat double precision,lon double precision,dms_lat varchar(6),dms_long varchar(7),utm varchar(4),jog varchar(7),fc varchar(1),dsg varchar(5),pc integer,cci varchar(2),adm1 varchar(2),adm2 varchar(200),dim integer,cc2 varchar(2),nt varchar(1),lc varchar(3),shortform varchar(128),generic varchar(128),sortname varchar(200),fullname varchar(200),funamesd varchar(200),mod_date date'
  116. rm -f "${TMPFILE}_ascii.csv"
  117. # write cmd history:
  118. v.support "$FILE" cmdhist="${CMDLINE}"
  119. exit 0