/[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 2356 - (show annotations)
Tue Sep 28 19:31:05 2004 UTC (20 years, 5 months ago) by jan
File MIME type: text/x-python
File size: 6996 byte(s)
Added registration of the extension.

1 # Copyright (C) 2003 by Intevation GmbH
2 # Authors:
3 # Jan-Oliver Wagner <[email protected]>
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 from wxPython.wx import *
29
30 from Thuban.UI.command import registry, Command
31 from Thuban.UI.mainwindow import main_menu
32 from Thuban import _
33 from Thuban.Model.layer import Layer
34
35 from Thuban.UI.extensionregistry import ExtensionDesc, ext_registry
36
37 import shapelib
38 import dbflib
39
40
41 ext_registry.add(ExtensionDesc(
42 name = 'gns2shp',
43 version = '1.0.0',
44 authors= [ 'Jan-Oliver Wagner' ],
45 copyright = '2003, 2004 Intevation GmbH',
46 desc = _("Converts GNS (Geographical Name Service\n" \
47 "of NIMA) to Shapefile format and\n" \
48 "displays the data.")))
49
50
51 def gns2shp(src_fname, dest_fname):
52 """Convert a file from gns textformat into a Shapefile.
53
54 The GNS text format is described on
55 http://www.nima.mil/gns/html/cntyfile/gis.html
56
57 src_fname -- Filename of the GNS standard textfile (including suffix '.txt)
58 dest_fname -- Filename where to write the Shapefile components (name
59 without suffix). After successful completion there
60 will be files "dest_fname".shp, "dest_fname".shx and
61 "dest_fname.dbf".
62
63 Return: Number of sucessfully converted entries
64 """
65 gns_filename = src_fname
66 shp_filename = dest_fname + '.shp'
67 dbf_filename = dest_fname + '.dbf'
68
69 gns = open(gns_filename, 'r').readlines()
70
71 shp = shapelib.create(shp_filename, shapelib.SHPT_POINT)
72 dbf = dbflib.create(dbf_filename)
73 dbf.add_field('RC', dbflib.FTInteger, 1, 0)
74 dbf.add_field('UFI', dbflib.FTInteger, 10, 0)
75 dbf.add_field('UNI', dbflib.FTInteger, 10, 0)
76 dbf.add_field('UTM', dbflib.FTString, 4, 0)
77 dbf.add_field('JOG', dbflib.FTString, 7, 0)
78 dbf.add_field('FC', dbflib.FTString, 1, 0)
79 dbf.add_field('DSG', dbflib.FTString, 5, 0)
80 dbf.add_field('PC', dbflib.FTInteger, 10, 0)
81 dbf.add_field('CC1', dbflib.FTString, 2, 0)
82 dbf.add_field('ADM1', dbflib.FTString, 2, 0)
83 dbf.add_field('ADM2', dbflib.FTString, 2, 0)
84 dbf.add_field('DIM', dbflib.FTInteger, 10, 0)
85 dbf.add_field('CC2', dbflib.FTString, 2, 0)
86 dbf.add_field('NT', dbflib.FTString, 1, 0)
87 dbf.add_field('LC', dbflib.FTString, 2, 0)
88 dbf.add_field('SHORT_FORM', dbflib.FTString, 40, 0)
89 dbf.add_field('GENERIC', dbflib.FTString, 40, 0)
90 dbf.add_field('SORT_NAME', dbflib.FTString, 40, 0)
91 dbf.add_field('FULL_NAME', dbflib.FTString, 40, 0)
92 dbf.add_field('FULL_ND', dbflib.FTString, 40, 0)
93 dbf.add_field('MODIFY_DATE', dbflib.FTString, 11, 0)
94 del dbf
95 dbf = dbflib.open(dbf_filename, 'r+b')
96
97 gns.pop(0) # drop the header line
98
99 i = 0
100 for line in gns:
101 if line[0] == '#': continue
102 RC, UFI, UNI, DD_LAT, DD_LONG, DMS_LAT, DMS_LONG, UTM, \
103 JOG, FC, DSG, PC, CC1, ADM1, ADM2, DIM, CC2, NT, LC, \
104 SHORT_FORM, GENERIC, SORT_NAME, FULL_NAME, FULL_NAME_ND, \
105 MODIFY_DATE = line.split('\t')
106 RC = int(RC)
107 UFI = int(UFI)
108 UNI = int(UNI)
109 DD_LAT = float(DD_LAT)
110 DD_LONG = float(DD_LONG)
111 try: PC = int(PC)
112 except: PC = None
113 try: DIM = int(DIM)
114 except: DIM = None
115 obj = shapelib.SHPObject(shapelib.SHPT_POINT, i, [[(DD_LONG, DD_LAT)]])
116 shp.write_object(-1, obj)
117 dbf.write_record(i, { 'RC': RC, 'UFI': UFI, 'UNI': UNI, 'UTM': UTM,
118 'JOG': JOG, 'FC': FC, 'DSG': DSG, 'PC': PC,
119 'CC1': CC1, 'ADM1': ADM1, 'ADM2': ADM2,
120 'DIM': DIM, 'CC2': CC2, 'NT': NT, 'LC': LC,
121 'SHORT_FORM': SHORT_FORM, 'GENERIC': GENERIC,
122 'SORT_NAME': SORT_NAME, 'FULL_NAME': FULL_NAME,
123 'FULL_ND': FULL_NAME_ND,
124 'MODIFY_DAT': MODIFY_DATE})
125 i += 1
126
127 del shp
128 del dbf
129
130 return i
131
132 def gns2shp_dialog(context):
133 """Request filename from user, run conversion and add
134 resulting shapefile to the current map.
135
136 context -- The Thuban context.
137 """
138 dlg = wxFileDialog(context.mainwindow,
139 _('Select GNS file'), '.', '',
140 _('Generate Files (*.txt)|*.txt|') +
141 _('All Files (*.*)|*.*'),
142 wxOPEN|wxOVERWRITE_PROMPT)
143 if dlg.ShowModal() == wxID_OK:
144 gns_filename = dlg.GetPath()
145 dlg.Destroy()
146 else:
147 return
148
149 no = gns2shp(gns_filename, gns_filename[:-4])
150 if no <= 0:
151 context.mainwindow.RunMessageBox(_('gns2shp'), _('Conversion failed'))
152 return
153 else:
154 context.mainwindow.RunMessageBox(_('gns2shp %s') % __version__,
155 _('%d locations converted' % no))
156
157 # Now load the newly created shapefile
158 filename = gns_filename[:-4] + '.shp'
159 title = os.path.splitext(os.path.basename(filename))[0]
160 map = context.mainwindow.canvas.Map()
161 has_layers = map.HasLayers()
162 try:
163 store = context.session.OpenShapefile(filename)
164 except IOError:
165 # the layer couldn't be opened
166 context.mainwindow.RunMessageBox(_('Add GNS Layer'),
167 _("Can't open the file '%s'.") % filename)
168 else:
169 layer = Layer(title, store)
170 map.AddLayer(layer)
171 if not has_layers:
172 # if we're adding a layer to an empty map, fit the
173 # new map to the window
174 context.mainwindow.canvas.FitMapToWindow()
175
176 if __name__ == '__main__': # gns2shp executed as a command line tool
177 print 'gns2shp.py %s' % __version__
178 if len(sys.argv) == 3:
179 no = gns2shp(sys.argv[1], sys.argv[2][:-4])
180 print '%d locations converted' % no
181 sys.exit(0)
182 else:
183 print 'usage: gns2shp.py GNS-file Shapefile'
184 sys.exit(1)
185
186
187 # gns2shp executed as an extension to Thuban
188
189 # register the new command
190 registry.Add(Command('gns2shp', _('gns2shp...'), gns2shp_dialog,
191 helptext = _('Convert GNS-file into a shapefile')))
192
193 # find the extensions menu (create it anew if not found)
194 extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
195
196 # finally add the new entry to the extensions menu
197 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