-
Notifications
You must be signed in to change notification settings - Fork 57
/
vectorize_map.py
executable file
·494 lines (388 loc) · 17.7 KB
/
vectorize_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/python
import os, logging, string, subprocess, re, ogr, numpy as np, osr
from map_vectorizer import detect
from map_vectorizer.config import parser
from map_vectorizer.util import average_color
def list_tiffs(inputfile):
# If input is a directory iterate through it
if os.path.isdir(inputfile) == True:
for ff in os.listdir(inputfile):
if ff.endswith(".tif"):
yield (ff, inputfile)
else:
# if input is a file, process it
# but first look to see if there is a path prepending it
if inputfile.endswith(".tif"):
yield (inputfile[inputfile.rfind("/")+1:], inputfile[:inputfile.rfind("/")+1])
def thresholdize(inputfile, dir_base_name):
args = parser.parse_args()
brightness = args.vectorize_config['brightness']
contrast = args.vectorize_config['contrast']
thresholdblack = args.vectorize_config['thresholdblack']
thresholdwhite = args.vectorize_config['thresholdwhite']
thresholdfile = dir_base_name + "-threshold-tmp.tif"
gimp_path = args.gimp_path
print "\n\n"
print "Thresholdizing:"
print "---------------"
print inputfile + " into threshold file: " + thresholdfile
contraststring = '(gimp-brightness-contrast drawable ' + str(brightness) + ' ' + str(contrast) + ')'
thresholdstring = '(gimp-threshold drawable ' + str(thresholdblack) + ' ' + str(thresholdwhite) + ')'
gimpcommand = '(let* ((image (car (file-tiff-load RUN-NONINTERACTIVE "' + inputfile + '" "' + inputfile + '"))) (drawable (car (gimp-image-get-layer-by-name image "Background")))) (gimp-selection-none image) ' + contraststring + ' ' + thresholdstring + ' (gimp-file-save RUN-NONINTERACTIVE image drawable "' + thresholdfile + '" "' + thresholdfile + '") (gimp-image-delete image))'
if (not os.path.isfile(thresholdfile)):
command = gimp_path + ' -i -b \'' + gimpcommand + '\' -b \'(gimp-quit 0)\''
logging.debug(command)
# print command
os.system(command)
outputwsg = dir_base_name + "-wsg-tmp.tif"
outputgdal = dir_base_name + "-gdal-tmp.tif"
# first get geotiff data from original
logging.debug( string.join(["gdalinfo", os.path.abspath(inputfile)]) )
geoText = subprocess.Popen(["gdalinfo", os.path.abspath(inputfile)], stdout=subprocess.PIPE).communicate()[0]
pattern = re.compile(r"Upper Left\s*\(\s*([0-9\-\.]*),\s*([0-9\-\.]*).*\n.*\n.*\nLower Right\s*\(\s*([0-9\-\.]*),\s*([0-9\-\.]*).*")
geoMatch = pattern.findall(geoText)
# print pattern
print "\n"
print "Geodata obtained:"
print "-----------------"
print "W", geoMatch[0][0]
print "N", geoMatch[0][1]
print "E", geoMatch[0][2]
print "S", geoMatch[0][3]
print "\n"
W = geoMatch[0][0]
N = geoMatch[0][1]
E = geoMatch[0][2]
S = geoMatch[0][3]
print "Applying to destination:"
print "------------------------"
# print outputgdal
if (not os.path.isfile(outputwsg)):
command = 'gdal_translate -a_srs "+proj=latlong +datum=WGS84" -of GTiff -co "INTERLEAVE=PIXEL" -a_ullr ' + W + ' ' + N + ' ' + E + ' ' + S + ' ' + thresholdfile + ' ' + outputwsg
logging.debug(command)
# print command
os.system(command)
print ""
if (not os.path.isfile(outputgdal)):
command = 'gdalwarp -s_srs EPSG:4326 -t_srs EPSG:3785 -r bilinear ' + outputwsg + ' ' + outputgdal
logging.debug(command)
# print command
os.system(command)
def polygonize(dir_base_name, base_name):
args = parser.parse_args()
chunksize = args.chunksize
currentchunk = 0
totalsubsets = 0
outputgdal = dir_base_name + "-gdal-tmp.tif"
# QGIS POLYGONIZE
print ""
print "Polygonizing (coarse):"
print "----------------------"
shapefile = dir_base_name + '.shp'
if (not os.path.isfile(shapefile)):
command = 'gdal_polygonize.py ' + outputgdal + ' -f "ESRI Shapefile" ' + shapefile + ' ' + base_name
logging.debug(command)
# print command
os.system(command)
# Split resulting megapolygon file into smaller chunks
# most code from: http://cosmicproject.org/OGR/cris_example_write.html
print ""
print "Splitting megapolygon file into chunks"
print "--------------------------------------"
#####
# 2 get the shapefile driver
driver = ogr.GetDriverByName('ESRI Shapefile')
# 3 open the input data source and get the layer
inDS = driver.Open(shapefile, 0) #shows cover at given points
if inDS is None:
print 'Could not open shapefile'
sys.exit(1)
inLayer = inDS.GetLayer()
# 5 get the FieldDefn's for the id and cover fields in the input shapefile
feature = inLayer.GetFeature(0)
idFieldDefn = feature.GetFieldDefnRef('DN')
# 7 loop through the input features
inFeature = inLayer.GetNextFeature()
while inFeature:
if currentchunk == 0 or currentchunk >= chunksize:
currentchunk = 0
totalsubsets = totalsubsets + 1
# this is a new temp file
# 4 create a new data source and layer
fn = dir_base_name + '-tmp-' + str(totalsubsets) + '.shp'
if os.path.exists(fn):driver.DeleteDataSource(fn)
outDS = driver.CreateDataSource(fn)
if outDS is None:
print 'Could not create temp shapefile'
sys.exit(1)
outLayer = outDS.CreateLayer(base_name, geom_type=ogr.wkbPolygon)
#create new field in the output shapefile
outLayer.CreateField(idFieldDefn)
# 6 get the FeatureDefn for the output layer
featureDefn = outLayer.GetLayerDefn()
# create a new feature
outFeature = ogr.Feature(featureDefn)#using featureDefn created in step 6
# set the geometry
geom = inFeature.GetGeometryRef()
outFeature.SetGeometry(geom) #move it to the new feature
# set the attributes
DN = inFeature.GetField('DN')
outFeature.SetField('DN', DN) #move it to the new feature
# add the feature to the output layer
outLayer.CreateFeature(outFeature)
# destroy the output feature
outFeature.Destroy()
# destroy the input feature and get a new one
inFeature.Destroy()
inFeature = inLayer.GetNextFeature()
currentchunk = currentchunk + 1
# close the data sources
inDS.Destroy()
outDS.Destroy() #flush out the last changes here
print ""
print "Produced " + str(totalsubsets) + " temporary shapefiles"
print ""
return totalsubsets
def simplify(path, base_name, totalsubsets):
# R Simplification
print ""
print "Polygonizing (simplify):"
print "------------------------"
# First simplify each temporary shapefile
currentsubset = 1
while currentsubset <= totalsubsets:
rinput = path + '/' + base_name + '-tmp-' + str(currentsubset) + '.shp'
routput = path + '/' + base_name + '-tmp-' # + str(currentsubset)
layer = base_name + '-tmp-' + str(currentsubset)
command = 'R --vanilla --silent --slave -f simplify_map.R --args ' + rinput + ' ' + layer + ' ' + routput + ' ' + path + ' ' + str(currentsubset)
logging.debug(command)
# print command
os.system(command)
currentsubset = currentsubset + 1
def consolidate(inputfile, path, dir_base_name, base_name):
# Now combine all subsets into a macroset
# 4 create a new data source and layer
fn = dir_base_name + '-traced.shp'
# 2 get the shapefile driver
driver = ogr.GetDriverByName('ESRI Shapefile')
# 3 open the input data source and get the layer
shapefile = dir_base_name + '.shp'
inDS = driver.Open(shapefile, 0) #shows cover at given points
if inDS is None:
print 'Could not open shapefile'
sys.exit(1)
inLayer = inDS.GetLayer()
# 5 get the FieldDefn's for the id and cover fields in the input shapefile
feature = inLayer.GetFeature(0)
idFieldDefn = feature.GetFieldDefnRef('DN')
if os.path.exists(fn):driver.DeleteDataSource(fn)
outDS = driver.CreateDataSource(fn)
if outDS is None:
print 'Could not create final shapefile'
sys.exit(1)
outLayer = outDS.CreateLayer(base_name, geom_type=ogr.wkbPolygon)
#create new field in the output shapefile
outLayer.CreateField(idFieldDefn)
# 6 get the FeatureDefn for the output layer
featureDefn = outLayer.GetLayerDefn()
# new field definitions for this shapefile
# color definition
colorDefn = ogr.FieldDefn("Color", ogr.OFTInteger)
colorDefn.SetWidth(2)
colorDefn.SetPrecision(0)
outLayer.CreateField( colorDefn )
# dot count definition
dotCountDefn = ogr.FieldDefn("DotCount", ogr.OFTInteger)
dotCountDefn.SetWidth(2)
dotCountDefn.SetPrecision(0)
outLayer.CreateField( dotCountDefn )
# dot type definition
dotTypeDefn = ogr.FieldDefn("DotType", ogr.OFTInteger)
dotTypeDefn.SetWidth(1)
dotTypeDefn.SetPrecision(0)
outLayer.CreateField( dotTypeDefn )
# cross count definition
crossCountDefn = ogr.FieldDefn("CrossCount", ogr.OFTInteger)
crossCountDefn.SetWidth(2)
crossCountDefn.SetPrecision(0)
outLayer.CreateField( crossCountDefn )
# cross data definition
crossDataDefn = ogr.FieldDefn("CrossData", ogr.OFTString)
crossDataDefn.SetWidth(255)
outLayer.CreateField( crossDataDefn )
# add lat/lon as OFTReal attributes
outLayer.CreateField(ogr.FieldDefn("CentroidY", ogr.OFTReal))
outLayer.CreateField(ogr.FieldDefn("CentroidX", ogr.OFTReal))
polygonfiles = []
for files in os.listdir(path):
if files.endswith(".shp") and files.find('-polygon') != -1:
polygonfile = path + "/" + files
# apply a projection so gdalwarp doesnt complain
polygonfilename = files[:files.find(".shp")]
os.system("cp " + dir_base_name + ".prj " + path + "/" + polygonfilename + ".prj")
extractedfile = path + "/" + polygonfilename + "-extracted.tif"
# extract bitmap from original
command = "gdalwarp -q -t_srs EPSG:3785 -cutline " + polygonfile + " -crop_to_cutline -of GTiff " + inputfile + " " + extractedfile
logging.debug(command)
# print command
os.system(command)
# calculate color
# shrink to 1x1 and find value
# logging.debug( string.join(["convert", "-quiet", os.path.abspath(extractedfile), "-resize", "1x1","txt:-"]) )
# pixelvalue = subprocess.Popen(["convert", "-quiet", os.path.abspath(extractedfile), "-resize", "1x1","txt:-"], stdout=subprocess.PIPE).communicate()[0]
# pattern = re.compile(r"0,0: \(([\s0-9]*),([\s0-9]*),([\s0-9]*).*")
# values = pattern.findall(pixelvalue)
extractedpath = os.path.abspath(extractedfile)
if os.path.exists(extractedpath) == False:
continue
values = average_color(extractedpath)
if len(values) > 0:
red = int(values[0])
green = int(values[1])
blue = int(values[2])
nearest = 100000
nearestcolor = []
nearestcolorindex = -1
args = parser.parse_args()
basecolors = args.vectorize_config['basecolors']
for i, color in enumerate(basecolors):
dred = (color[0] - red) * (color[0] - red)
dgreen = (color[1] - green) * (color[1] - green)
dblue = (color[2] - blue) * (color[2] - blue)
dist = dred + dgreen + dblue
if dist < nearest:
nearest = dist
nearestcolor = color
nearestcolorindex = i
# only add if NOT paper
if nearestcolor != basecolors[0]:
# check for dots
circle_data = cv_feature_detect(extractedfile)
# add to array
polygonfiles.append([polygonfile, nearestcolorindex, circle_data])
else:
logging.debug("Ignored (paper color): " + polygonfilename + "\n")
else:
logging.debug("Ignored (regex match error): " + polygonfilename + "\n")
for files in polygonfiles:
# 3 open the input data source and get the layer
tempfile = files[0] #dir_base_name + '-tmp-' + str(currentsubset) + '-traced.shp'
inDS = driver.Open(tempfile, 0) #shows cover at given points
if inDS is None:
print 'Could not open temporary shapefile'
break
inLayer = inDS.GetLayer()
# 7 loop through the input features
inFeature = inLayer.GetNextFeature()
while inFeature:
# create a new feature
outFeature = ogr.Feature(featureDefn) #using featureDefn created in step 6
# set the geometry
geom = inFeature.GetGeometryRef()
outFeature.SetGeometry(geom) #move it to the new feature
DN = inFeature.GetField('DN')
outFeature.SetField('DN', DN ) #move it to the new feature
outFeature.SetField('Color', int(files[1]) )
outFeature.SetField('DotCount', int(files[2]["count"]) )
outFeature.SetField('DotType', int(files[2]["is_outline"]) )
outFeature.SetField('CrossCount', int(files[2]["cross_count"]) )
outFeature.SetField('CrossData', str(files[2]["cross_data"]) )
source_srs = osr.SpatialReference()
source_srs.ImportFromEPSG(3785) # NOTE: notice this is hardcoded
target_srs = osr.SpatialReference()
target_srs.ImportFromEPSG(4326) # NOTE: notice this is hardcoded
transform = osr.CoordinateTransformation(source_srs, target_srs)
centroid = geom.Centroid()
centroid.Transform(transform)
outFeature.SetField('CentroidY', centroid.GetY())
outFeature.SetField('CentroidX', centroid.GetX())
# outFeature.SetField('circle_count', files[2]["circle_count"])
# outFeature.SetField('circle_type', files[2]["is_outline"])
# add the feature to the output layer
outLayer.CreateFeature(outFeature)
# destroy the output feature
outFeature.Destroy()
# destroy the input feature and get a new one
inFeature.Destroy()
inFeature = inLayer.GetNextFeature()
# close the data sources
inDS.Destroy()
outDS.Destroy() #flush out the last changes here
print ""
print "Applying projection file to result..."
print "-------------------------------------"
os.system("cp " + dir_base_name + ".prj " + dir_base_name + "-traced.prj")
def process_file(inputfile, basedir = ""):
"""NOTE: This still needs a lot of work for when dealing
with subfolders and such.
Best case is image file is located in same dir as vectorizer_map.py
"""
args = parser.parse_args()
gimp_path = args.gimp_path
print "\n\nProcessing file: " + inputfile
# right now assuming vectorizer, simplifier and input are in the same folder
fullpath = os.path.abspath(__file__)
base_name = inputfile[:inputfile.find(".tif")]
base_name = base_name[base_name.rfind("/")+1:]
# create a folder to store all this
if basedir != '':
directory = basedir + '/' + base_name
inputfile = basedir + '/' + inputfile
else:
directory = base_name
if not os.path.exists(directory):
os.makedirs(directory)
path = os.path.abspath(directory)#fullpath[:fullpath.find("/vectorize_map.py")] + '/' + directory
# GIMP processing
dir_base_name = os.path.join(directory, base_name)
# create a log file
# logfile = open(directory + "/py-log.txt", "w")
logging.basicConfig(filename=os.path.join(directory, "py-log.txt"),
format='%(asctime)s %(message)s',level=logging.DEBUG)
logging.debug("Log file for " + inputfile + " with colors:\n\n")
logging.debug(str(args.vectorize_config['basecolors']) + "\n\n")
thresholdize(inputfile, dir_base_name)
totalsubsets = polygonize(dir_base_name, base_name)
simplify(path, base_name, totalsubsets)
consolidate(inputfile, path, dir_base_name, base_name)
print ""
print "Creating GeoJSON output..."
print "--------------------------"
jsonfile = dir_base_name + '-traced.json'
shapefile = dir_base_name + '-traced.shp'
command = 'ogr2ogr -t_srs EPSG:4326 -s_srs EPSG:3857 -f "GeoJSON" ' + jsonfile + ' ' + shapefile
logging.debug(command)
# print command
os.system(command)
# Cleaning
print ""
print "Cleaning..."
print "-----------"
os.system("rm " + dir_base_name + "-gdal-tmp.tif")
os.system("rm " + dir_base_name + "-wsg-tmp.tif")
os.system("rm " + dir_base_name + "-threshold-tmp.tif")
os.system("rm " + dir_base_name + "-tmp-*.shp")
os.system("rm " + dir_base_name + "-tmp-*.dbf")
os.system("rm " + dir_base_name + "-tmp-*.shx")
os.system("rm " + dir_base_name + "-tmp-*.prj")
os.system("rm " + dir_base_name + "-tmp*.tif")
os.system("rm " + dir_base_name + ".*")
# close log file
logging.shutdown()
def cv_feature_detect(inputfile):
result = {}
circles = detect.circles(inputfile)
result["count"] = circles["count"]
result["is_outline"] = circles["is_outline"]
result["circles"] = circles["circles"]
crosses = detect.crosses(inputfile)
result["cross_count"] = crosses["count"]
result["cross_data"] = crosses["data"]
return result
def main():
args = parser.parse_args()
for i, (ff, inputfile) in enumerate(list_tiffs(args.inputfile)):
process_file(ff, inputfile)
print('Processed %d file(s)' % (i+1))
if __name__ == "__main__":
main()