/[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 2389 - (show annotations)
Tue Nov 16 21:15:17 2004 UTC (20 years, 3 months ago) by jan
File MIME type: text/x-python
File size: 6634 byte(s)
Removed registry entry (moved to __init__.py).

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