/[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 2817 - (show annotations)
Sun Jan 27 00:01:32 2008 UTC (17 years, 1 month ago) by bernhard
File MIME type: text/x-python
File size: 6778 byte(s)
Changed a few places in Thuban and Extensions to convert wx
strings to internal string encoding. 

* Thuban/UI/classgen.py: Checked for return strings from wx problems.
Added one clarifing comment, removed two unused variables.

* Thuban/UI/mainwindow.py(OpenSession, SaveSessionAs, TableOpen): Using 
internal_from_wxstring for the pathname.

* Thuban/UI/tableview.py(doExport):  Using internal_from_wxstring
for the pathname.

* Thuban/UI/view.py(MapCanvas.Export):  Using internal_from_wxstring
for the pathname.

* Extensions/bboxdump/bboxdump.py(OnSelectFilename),
Extensions/export_shapefile/export_shapefile.py,
Extensions/gns2shp/gns2shp.py, Extensions/importAPR/importAPR.py,
Extensions/svgexport/maplegend.py, Extensions/svgexport/svgsaver.py,
Extensions/umn_mapserver/mf_import.py: Using
internal_from_wxstring for the pathname.

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 dbf.add_field('RC', dbflib.FTInteger, 1, 0)
62 dbf.add_field('UFI', dbflib.FTInteger, 10, 0)
63 dbf.add_field('UNI', dbflib.FTInteger, 10, 0)
64 dbf.add_field('UTM', dbflib.FTString, 4, 0)
65 dbf.add_field('JOG', dbflib.FTString, 7, 0)
66 dbf.add_field('FC', dbflib.FTString, 1, 0)
67 dbf.add_field('DSG', dbflib.FTString, 5, 0)
68 dbf.add_field('PC', dbflib.FTInteger, 1, 0)
69 dbf.add_field('CC1', dbflib.FTString, 2, 0)
70 dbf.add_field('ADM1', dbflib.FTString, 2, 0)
71 dbf.add_field('ADM2', dbflib.FTString, 200, 0)
72 dbf.add_field('DIM', dbflib.FTInteger, 10, 0)
73 dbf.add_field('CC2', dbflib.FTString, 2, 0)
74 dbf.add_field('NT', dbflib.FTString, 1, 0)
75 dbf.add_field('LC', dbflib.FTString, 2, 0)
76 dbf.add_field('SHORT_FORM', dbflib.FTString, 128, 0)
77 dbf.add_field('GENERIC', dbflib.FTString, 128, 0)
78 dbf.add_field('SORT_NAME', dbflib.FTString, 200, 0)
79 dbf.add_field('FULL_NAME', dbflib.FTString, 200, 0)
80 dbf.add_field('FULL_ND', dbflib.FTString, 200, 0) # FULL_NAME_ND
81 dbf.add_field('MODIFY_DATE', dbflib.FTString, 11, 0)
82 del dbf
83 dbf = dbflib.open(dbf_filename, 'r+b')
84
85 gns.pop(0) # drop the header line
86
87 i = 0
88 for line in gns:
89 if line[0] == '#': continue
90 RC, UFI, UNI, DD_LAT, DD_LONG, DMS_LAT, DMS_LONG, UTM, \
91 JOG, FC, DSG, PC, CC1, ADM1, ADM2, DIM, CC2, NT, LC, \
92 SHORT_FORM, GENERIC, SORT_NAME, FULL_NAME, FULL_NAME_ND, \
93 MODIFY_DATE = line.split('\t')
94 RC = int(RC)
95 UFI = int(UFI)
96 UNI = int(UNI)
97 DD_LAT = float(DD_LAT)
98 DD_LONG = float(DD_LONG)
99 try: PC = int(PC)
100 except: PC = None
101 try: DIM = int(DIM)
102 except: DIM = None
103 MODIFY_DATE = MODIFY_DATE[0:10] # kill trailing "\n" or "\r\n"
104 obj = shapelib.SHPObject(shapelib.SHPT_POINT, i, [[(DD_LONG, DD_LAT)]])
105 shp.write_object(-1, obj)
106 dbf.write_record(i, { 'RC': RC, 'UFI': UFI, 'UNI': UNI, 'UTM': UTM,
107 'JOG': JOG, 'FC': FC, 'DSG': DSG, 'PC': PC,
108 'CC1': CC1, 'ADM1': ADM1, 'ADM2': ADM2,
109 'DIM': DIM, 'CC2': CC2, 'NT': NT, 'LC': LC,
110 'SHORT_FORM': SHORT_FORM, 'GENERIC': GENERIC,
111 'SORT_NAME': SORT_NAME, 'FULL_NAME': FULL_NAME,
112 'FULL_ND': FULL_NAME_ND,
113 'MODIFY_DAT': MODIFY_DATE})
114 i += 1
115
116 del shp
117 del dbf
118
119 return i
120
121 def gns2shp_dialog(context):
122 """Request filename from user, run conversion and add
123 resulting shapefile to the current map.
124
125 context -- The Thuban context.
126 """
127 dlg = wx.FileDialog(context.mainwindow,
128 _('Select GNS file'), '.', '',
129 _('Generate Files (*.txt)|*.txt|') +
130 _('All Files (*.*)|*.*'),
131 wx.OPEN|wx.OVERWRITE_PROMPT)
132 if dlg.ShowModal() == wx.ID_OK:
133 gns_filename = internal_from_wxstring(dlg.GetPath())
134 dlg.Destroy()
135 else:
136 return
137
138 no = gns2shp(gns_filename, gns_filename[:-4])
139 if no <= 0:
140 context.mainwindow.RunMessageBox(_('gns2shp'), _('Conversion failed'))
141 return
142 else:
143 context.mainwindow.RunMessageBox(_('gns2shp %s') % __version__,
144 _('%d locations converted' % no))
145
146 # Now load the newly created shapefile
147 filename = gns_filename[:-4] + '.shp'
148 title = os.path.splitext(os.path.basename(filename))[0]
149 map = context.mainwindow.canvas.Map()
150 has_layers = map.HasLayers()
151 try:
152 store = context.session.OpenShapefile(filename)
153 except IOError:
154 # the layer couldn't be opened
155 context.mainwindow.RunMessageBox(_('Add GNS Layer'),
156 _("Can't open the file '%s'.") % filename)
157 else:
158 layer = Layer(title, store)
159 map.AddLayer(layer)
160 if not has_layers:
161 # if we're adding a layer to an empty map, fit the
162 # new map to the window
163 context.mainwindow.canvas.FitMapToWindow()
164
165 if __name__ == '__main__': # gns2shp executed as a command line tool
166 print 'gns2shp.py %s' % __version__
167 if len(sys.argv) == 3:
168 no = gns2shp(sys.argv[1], sys.argv[2][:-4])
169 print '%d locations converted' % no
170 sys.exit(0)
171 else:
172 print 'usage: gns2shp.py GNS-file Shapefile'
173 sys.exit(1)
174
175
176 # gns2shp executed as an extension to Thuban
177
178 # register the new command
179 registry.Add(Command('gns2shp', _('gns2shp...'), gns2shp_dialog,
180 helptext = _('Convert GNS-file into a shapefile')))
181
182 # find the extensions menu (create it anew if not found)
183 extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
184
185 # finally add the new entry to the extensions menu
186 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