/[thuban]/trunk/thuban/Extensions/gns2shp/gns2shp.py
ViewVC logotype

Contents of /trunk/thuban/Extensions/gns2shp/gns2shp.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2845 - (show annotations)
Fri Jun 20 05:20:03 2008 UTC (16 years, 8 months ago) by elachuni
File MIME type: text/x-python
File size: 6838 byte(s)
Updating gns2shp to support the new GNS file format.


1 # Copyright (C) 2003, 2004 by Intevation GmbH
2 # Authors:
3 # Jan-Oliver Wagner <[email protected]> (2003, 2004)
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 """
9 Extend thuban with a gns2shp converter.
10
11 This module also works without Thuban on the command line
12 if the shapelib and dbflib python wrappers can be found via the
13 PYTHONPATH (e.g. via including directory 'Lib' of thuban directory).
14
15 The GUI should eventually be extended to allow pre-selection
16 of the points to import in order to reduce immense quantities
17 of some country files.
18 """
19
20 __version__ = '$Revision$'
21 # $Source$
22 # $Id$
23
24 import os, sys
25
26 # only import GUI and register when not called as command line tool
27 if __name__ != '__main__':
28 import wx
29
30 from Thuban.UI import internal_from_wxstring
31 from Thuban.UI.command import registry, Command
32 from Thuban.UI.mainwindow import main_menu
33 from Thuban import _
34 from Thuban.Model.layer import Layer
35
36 import shapelib
37 import dbflib
38
39 def gns2shp(src_fname, dest_fname):
40 """Convert a file from gns textformat into a Shapefile.
41
42 The GNS text format is described on
43 http://www.nima.mil/gns/html/gis.html
44
45 src_fname -- Filename of the GNS standard textfile (including suffix '.txt)
46 dest_fname -- Filename where to write the Shapefile components (name
47 without suffix). After successful completion there
48 will be files "dest_fname".shp, "dest_fname".shx and
49 "dest_fname.dbf".
50
51 Return: Number of sucessfully converted entries
52 """
53 gns_filename = src_fname
54 shp_filename = dest_fname + '.shp'
55 dbf_filename = dest_fname + '.dbf'
56
57 gns = open(gns_filename, 'r').readlines()
58
59 shp = shapelib.create(shp_filename, shapelib.SHPT_POINT)
60 dbf = dbflib.create(dbf_filename)
61
62 dbf.add_field('RC', dbflib.FTInteger, 1, 0)
63 dbf.add_field('UFI', dbflib.FTInteger, 10, 0)
64 dbf.add_field('UNI', dbflib.FTInteger, 10, 0)
65 dbf.add_field('MGRS', dbflib.FTString, 15, 0)
66 dbf.add_field('JOG', dbflib.FTString, 7, 0)
67 dbf.add_field('FC', dbflib.FTString, 1, 0)
68 dbf.add_field('DSG', dbflib.FTString, 5, 0)
69 dbf.add_field('PC', dbflib.FTInteger, 1, 0)
70 dbf.add_field('CC1', dbflib.FTString, 2, 0)
71 dbf.add_field('ADM1', dbflib.FTString, 2, 0)
72 dbf.add_field('ADM2', dbflib.FTString, 200, 0)
73 dbf.add_field('POP', dbflib.FTInteger, 10, 0)
74 dbf.add_field('ELEV', dbflib.FTInteger, 10, 0)
75 dbf.add_field('CC2', dbflib.FTString, 2, 0)
76 dbf.add_field('NT', dbflib.FTString, 2, 0)
77 dbf.add_field('LC', dbflib.FTString, 3, 0)
78 dbf.add_field('SHORT_FORM', dbflib.FTString, 128, 0)
79 dbf.add_field('GENERIC', dbflib.FTString, 128, 0)
80 dbf.add_field('SORT_NAME', dbflib.FTString, 200, 0)
81 dbf.add_field('FULL_NAME', dbflib.FTString, 200, 0)
82 dbf.add_field('FULL_ND', dbflib.FTString, 200, 0) # FULL_NAME_ND
83 dbf.add_field('MODIFY_DATE', dbflib.FTString, 11, 0)
84 del dbf
85 dbf = dbflib.open(dbf_filename, 'r+b')
86
87 gns.pop(0) # drop the header line
88
89 i = 0
90 for line in gns:
91 if line[0] == '#': continue
92 RC, UFI, UNI, LAT, LONG, DMS_LAT, DMS_LONG, MGRS, JOG, FC, DSG, PC, \
93 CC1, ADM1, ADM2, POP, ELEV, CC2, NT, LC, SHORT_FORM, GENERIC, \
94 SORT_NAME, FULL_NAME, FULL_NAME_ND, MODIFY_DATE = line.split('\t')
95 try: RC = int(RC)
96 except ValueError: RC = None
97 UFI = int(UFI)
98 UNI = int(UNI)
99 LAT = float(LAT)
100 LONG = float(LONG)
101 try: PC = int(PC)
102 except ValueError: PC = None
103 try: POP = int(POP)
104 except ValueError: POP = None
105 try: ELEV = int(ELEV)
106 except ValueError: ELEV = None
107 MODIFY_DATE = MODIFY_DATE[0:10] # kill trailing "\n" or "\r\n"
108 obj = shapelib.SHPObject(shapelib.SHPT_POINT, i, [[(LONG, LAT)]])
109 shp.write_object(-1, obj)
110
111 vals = {'RC': RC, 'UFI': UFI, 'UNI': UNI, 'MGRS':MGRS, 'JOG': JOG,
112 'FC': FC, 'DSG': DSG, 'PC': PC, 'CC1': CC1, 'ADM1': ADM1,
113 'ADM2': ADM2, 'POP': POP, 'ELEV': ELEV, 'CC2': CC2, 'NT': NT,
114 'LC': LC, 'SHORT_FORM': SHORT_FORM, 'GENERIC': GENERIC,
115 'SORT_NAME': SORT_NAME, 'FULL_NAME': FULL_NAME,
116 'FULL_ND': FULL_NAME_ND, 'MODIFY_DAT': MODIFY_DATE}
117 dbf.write_record(i, vals)
118 i += 1
119
120 del shp
121 del dbf
122
123 return i
124
125 def gns2shp_dialog(context):
126 """Request filename from user, run conversion and add
127 resulting shapefile to the current map.
128
129 context -- The Thuban context.
130 """
131 dlg = wx.FileDialog(context.mainwindow,
132 _('Select GNS file'), '.', '',
133 _('Generate Files (*.txt)|*.txt|') +
134 _('All Files (*.*)|*.*'),
135 wx.OPEN|wx.OVERWRITE_PROMPT)
136 if dlg.ShowModal() == wx.ID_OK:
137 gns_filename = internal_from_wxstring(dlg.GetPath())
138 dlg.Destroy()
139 else:
140 return
141
142 no = gns2shp(gns_filename, gns_filename[:-4])
143 if no <= 0:
144 context.mainwindow.RunMessageBox(_('gns2shp'), _('Conversion failed'))
145 return
146 else:
147 context.mainwindow.RunMessageBox(_('gns2shp %s') % __version__,
148 _('%d locations converted' % no))
149
150 # Now load the newly created shapefile
151 filename = gns_filename[:-4] + '.shp'
152 title = os.path.splitext(os.path.basename(filename))[0]
153 map = context.mainwindow.canvas.Map()
154 has_layers = map.HasLayers()
155 try:
156 store = context.session.OpenShapefile(filename)
157 except IOError:
158 # the layer couldn't be opened
159 context.mainwindow.RunMessageBox(_('Add GNS Layer'),
160 _("Can't open the file '%s'.") % filename)
161 else:
162 layer = Layer(title, store)
163 map.AddLayer(layer)
164 if not has_layers:
165 # if we're adding a layer to an empty map, fit the
166 # new map to the window
167 context.mainwindow.canvas.FitMapToWindow()
168
169 if __name__ == '__main__': # gns2shp executed as a command line tool
170 print 'gns2shp.py %s' % __version__
171 if len(sys.argv) == 3:
172 no = gns2shp(sys.argv[1], sys.argv[2][:-4])
173 print '%d locations converted' % no
174 sys.exit(0)
175 else:
176 print 'usage: gns2shp.py GNS-file Shapefile'
177 sys.exit(1)
178
179
180 # gns2shp executed as an extension to Thuban
181
182 # register the new command
183 registry.Add(Command('gns2shp', _('gns2shp...'), gns2shp_dialog,
184 helptext = _('Convert GNS-file into a shapefile')))
185
186 # find the extensions menu (create it anew if not found)
187 extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
188
189 # finally add the new entry to the extensions menu
190 extensions_menu.InsertItem('gns2shp')

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26