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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1721 - (hide annotations)
Mon Sep 22 10:32:04 2003 UTC (21 years, 5 months ago) by jan
File MIME type: text/x-python
File size: 6690 byte(s)
The main module of gns2shp.

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