Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 113 lines (83 sloc) 4.191 kb
5410d85 @onyxfish Fix shebangs. (sigh)
authored
1 #!/usr/bin/env python
19ba683 @onyxfish Added non-functional ivframe.
authored
2
3 import csv
4 import multiprocessing
5 import os
6 import sys
7
8 import invar
9
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
10 DEFAULT_FRAME_ZOOM = 16
19ba683 @onyxfish Added non-functional ivframe.
authored
11
b8b7cca Refactored iframe onto common base.
cgroskopf authored
12 class IVFrame(invar.InvarUtility):
13 description = 'Render centered frames from a Mapnik2 XML configuration file. Accepts either a single latitude/longitude pair or the name of a CSV file containing appropriate columns.'
14
15 def add_arguments(self):
16 self.argparser.add_argument('latitude', help='Latitude to center rendering on.', type=float, nargs='?')
17 self.argparser.add_argument('longitude', help='Longitude to center rendering on.', type=float, nargs='?')
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
18 self.argparser.add_argument('-z', '--zoom', help="Zoom level to render.", type=int, default=DEFAULT_FRAME_ZOOM)
b8b7cca Refactored iframe onto common base.
cgroskopf authored
19 self.argparser.add_argument('--csv', help='A CSV file containing at latitude and longitude columns.')
90f53b6 @onyxfish Make SVG output work. (hacky).
authored
20 self.argparser.add_argument('-n', '--name', help='The name of the output image or the name of a column in the CSV file to use as a filename.')
b8b7cca Refactored iframe onto common base.
cgroskopf authored
21
22 def main(self):
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
23 # Get a list of tile parameter tuples to be added to the tile queue
b8b7cca Refactored iframe onto common base.
cgroskopf authored
24 if self.args.csv:
25 tile_parameters = get_tile_parameters_from_csv(self.args.csv, self.args.zoom, self.args.name)
26 else:
27 if not self.args.latitude or not self.args.longitude:
28 sys.exit('You must specify a latitude and longitude unless providing the name of a CSV.')
29
4f2ded8 @onyxfish Add examples and release process.
authored
30 filename = self.args.name or 'frame.%s' % invar.constants.FILE_TYPE_EXTS[self.args.file_type]
31
32 tile_parameters = [(filename, self.args.latitude, self.args.longitude, self.args.zoom)]
b8b7cca Refactored iframe onto common base.
cgroskopf authored
33
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
34 if not os.path.isdir(self.args.output_dir):
35 os.mkdir(self.args.output_dir)
b8b7cca Refactored iframe onto common base.
cgroskopf authored
36
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
37 tile_queue = multiprocessing.JoinableQueue()
38 tile_count = 0
19ba683 @onyxfish Added non-functional ivframe.
authored
39
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
40 for i, t in enumerate(tile_parameters):
41 tile_queue.put(t)
42 tile_count += 1
19ba683 @onyxfish Added non-functional ivframe.
authored
43
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
44 print 'Using %i processes to render %i tiles' % (self.args.process_count, tile_count)
19ba683 @onyxfish Added non-functional ivframe.
authored
45
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
46 processes = []
19ba683 @onyxfish Added non-functional ivframe.
authored
47
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
48 for i in range(self.args.process_count):
370fc74 @onyxfish Better file for tile queuing.
authored
49 renderer = invar.FrameRenderer(self.args.output_dir, [tile_queue], self.args.config, self.args.width, self.args.height, filetype=self.args.file_type, buffer_size=self.args.buffer, skip_existing=self.args.skip_existing)
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
50 renderer.start()
19ba683 @onyxfish Added non-functional ivframe.
authored
51
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
52 processes.append(renderer)
19ba683 @onyxfish Added non-functional ivframe.
authored
53
980c7d7 Added support for custom width and height. Refactored code to be more re...
cgroskopf authored
54 try:
55 tile_queue.join()
56 except KeyboardInterrupt:
57 for p in processes:
58 p.terminate()
19ba683 @onyxfish Added non-functional ivframe.
authored
59
c444801 ivframe w/ working CSV data source.
cgroskopf authored
60 def get_tile_parameters_from_csv(filename, zoom, name_column=None):
19ba683 @onyxfish Added non-functional ivframe.
authored
61 """
62 Finds latitude and longitude columns in a CSV and extracts coordinate pairs from them.
63
6f8e5de Convert get_tile_parameters_from_csv to be a generator function.
cgroskopf authored
64 Note: this is a generator function.
65 """
19ba683 @onyxfish Added non-functional ivframe.
authored
66 with open(filename, 'rU') as f:
67 rows = csv.reader(f)
68 headers = rows.next()
69 headers = [s.lower().strip() for s in headers]
70
71 latitude_index = None
72 longitude_index = None
c444801 ivframe w/ working CSV data source.
cgroskopf authored
73 name_index = None
19ba683 @onyxfish Added non-functional ivframe.
authored
74
75 for n in ['latitude', 'lat', 'y']:
76 if n in headers:
77 latitude_index = headers.index(n)
78
c444801 ivframe w/ working CSV data source.
cgroskopf authored
79 if latitude_index < 0:
19ba683 @onyxfish Added non-functional ivframe.
authored
80 sys.exit('Unable to find latitude column in CSV.')
81
82 for n in ['longitude', 'lon', 'x']:
83 if n in headers:
84 longitude_index = headers.index(n)
85
c444801 ivframe w/ working CSV data source.
cgroskopf authored
86 if longitude_index < 0:
19ba683 @onyxfish Added non-functional ivframe.
authored
87 sys.exit('Unable to find longitude column in CSV.')
88
c444801 ivframe w/ working CSV data source.
cgroskopf authored
89 if name_column:
90 name_index = headers.index(name_column)
91
92 if name_index < 0:
93 sys.exit('Column "%s" does not exist in the input CSV.' % name_column)
94
95 for i, row in enumerate(rows):
19ba683 @onyxfish Added non-functional ivframe.
authored
96 try:
97 latitude = float(row[latitude_index].strip())
98 longitude = float(row[longitude_index].strip())
99 except ValueError:
100 print 'Skipping non-numeric latitude/longitude: %s, %s' % (row[latitude_index], row[longitude_index])
101 continue
102
c444801 ivframe w/ working CSV data source.
cgroskopf authored
103 if name_index:
90f53b6 @onyxfish Make SVG output work. (hacky).
authored
104 filename = row[name_index]
c444801 ivframe w/ working CSV data source.
cgroskopf authored
105 else:
90f53b6 @onyxfish Make SVG output work. (hacky).
authored
106 filename = str(i)
c444801 ivframe w/ working CSV data source.
cgroskopf authored
107
90f53b6 @onyxfish Make SVG output work. (hacky).
authored
108 yield (filename, latitude, longitude, zoom)
19ba683 @onyxfish Added non-functional ivframe.
authored
109
110 if __name__ == "__main__":
b8b7cca Refactored iframe onto common base.
cgroskopf authored
111 ivframe = IVFrame()
112 ivframe.main()
Something went wrong with that request. Please try again.