forked from nypl-spacetime/map-vectorizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorize_map.py
executable file
·629 lines (511 loc) · 21.3 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/python
import re
import sys
import getopt
import subprocess
import shlex
import os
import datetime
import ogr
import glob
import csv
import cv2
import logging
import string
import numpy as np
from cv2 import cv
from config import *
def setup_gimp():
global gimp_path
global basecolors
global brightness
global contrast
global thresholdblack
global thresholdwhite
global starttime
gimp_path = raw_input("GIMP executable path [" + defaultgimp + "]: ")
starttime = datetime.datetime.now()
config_file = "vectorize_config.txt"
if gimp_path == '':
gimp_path = defaultgimp
if os.path.isfile(config_file):
tempcolors = []
index = 0
with open(config_file, 'r') as configcsv:
configdata = csv.reader(configcsv, delimiter=',')
for row in configdata:
if index > 0:
tempcolors.append([int(row[0]), int(row[1]), int(row[2])])
else:
# brightness/contrast/threshold values
brightness = int(row[0])
contrast = int(row[1])
thresholdblack = int(row[2])
thresholdwhite = int(row[3])
index = index + 1
if len(tempcolors) > 2:
basecolors = tempcolors
def process(inputfile):
totalfiles = 0
# If input is a directory iterate through it
if os.path.isdir(inputfile) == True:
for ff in os.listdir(inputfile):
if ff.endswith(".tif"):
totalfiles = totalfiles + 1
process_file(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"):
process_file(inputfile[inputfile.rfind("/")+1:], inputfile[:inputfile.rfind("/")+1])
totalfiles = 1
endtime = datetime.datetime.now()
deltatime = endtime - starttime
print "Processed " + str(totalfiles) + " files\n"
print "Operation took " + str(deltatime.seconds) + " seconds"
def thresholdize(inputfile):
thresholdfile = dir_base_name + "-threshold-tmp.tif"
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)
tempgdalfile = dir_base_name + "-tmp.tif"
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():
global currentchunk
global totalsubsets
global base_name
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 ""
def simplify():
# 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):
# 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 )
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)
if len(values) > 0:
red = int(values[0][0])
green = int(values[0][1])
blue = int(values[0][2])
nearest = 100000
nearestcolor = []
nearestcolorindex = -1
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"]) )
# 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
"""
global tempgdalfile
global instructions
global defaultgimp
global gimp_path
global directory
global path
global dir_base_name
global base_name
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 = directory + "/" + base_name
# create a log file
# logfile = open(directory + "/py-log.txt", "w")
logging.basicConfig(filename=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(basecolors) + "\n\n")
thresholdize(inputfile)
polygonize()
simplify()
consolidate(inputfile)
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
# logfile.close()
def detect_crosses(im, gray):
# NOW DETECT CROSSES
# code based on http://nbviewer.ipython.org/5861365
score_threshold = 0.954 # certainty there IS a cross
cross1 = cv2.imread("cross1.jpg")
cross_count = 0
cross_data = {}
if cross1.shape[0] < im.shape[0] and cross1.shape[1] < im.shape[1]:
graycross1 = cv2.cvtColor(cross1,cv.CV_RGB2GRAY)
match1 = cv2.matchTemplate(gray, graycross1, cv2.TM_CCORR_NORMED)
min_score, max_score, (min_x, min_y), (max_x, max_y) = cv2.minMaxLoc(match1)
if (max_score >= score_threshold):
# only testing 1 cross for now
cross_count = 1
corner_topL = (max_x, max_y)
corner_botR = (corner_topL[0]+cross1.shape[1], corner_topL[1]+cross1.shape[0])
cross_data = {"top_left":corner_topL, "bottom_right":corner_botR, "score": max_score}
return {"count":cross_count, "data": cross_data}
def detect_circles(im, gray):
max_dist = 20 # distance between circles to consider it an empty circle
circles = cv2.HoughCircles(gray, cv.CV_HOUGH_GRADIENT, 1, 2, np.array([]), 200, 8, 4, 8)
total_circles = 0
outline_circles = 1
unique_circles = []
if not (isinstance(circles, np.ndarray) and circles.shape[1] > 0):
return {"count":0, "is_outline": 0, "circles":circles}
else:
total_circles = circles.shape[1]
if total_circles == 1:
# only one circle and it is filled
return {"count":total_circles, "is_outline": 0, "circles":circles}
else :
# this is wrong... use for now
outline_circles = 0
if total_circles > 0:
current_circle = -1
current_x = circles[0][0][0]
current_y = circles[0][0][1]
# an array of circles with distance less than max_dist
# starts with the first circle
unique_circles = [[current_x, current_y]]
delta_x = 0
delta_y = 0
for n in range(1, total_circles):
circle = circles[0][n]
current_x = circle[0]
current_y = circle[1]
# distance to all the unique circles
last_unique = circle
is_inside = False
for unique in unique_circles:
last_unique = unique
delta_x = unique[0] - current_x
delta_y = unique[1] - current_y
square_dist = (delta_x*delta_x) + (delta_y*delta_y)
if square_dist <= max_dist:
# circle is inside another unique
is_inside = True
# we assume all are outlines if at least one is outline
outline_circles = 1
break
if not is_inside:
unique_circles.append([current_x, current_y])
# cv2.circle(im,(circle[0],circle[1]),circle[2],(0,0,255), 1)
return {"count":len(unique_circles), "is_outline": outline_circles, "circles":circles}
def cv_feature_detect(inputfile):
result = {}
im=cv2.imread(inputfile)
gray=cv2.cvtColor(im,cv.CV_RGB2GRAY)
circles = detect_circles(im, gray)
result["count"] = circles["count"]
result["is_outline"] = circles["is_outline"]
result["circles"] = circles["circles"]
crosses = detect_crosses(im, gray)
result["cross_count"] = crosses["count"]
result["cross_data"] = crosses["data"]
return result
def main(argv):
global instructions
global defaultgimp
global gimp_path
global basecolors
global brightness
global contrast
global thresholdblack
global thresholdwhite
global starttime
# Process CLI args
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print instructions
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print instructions
sys.exit()
elif opt in ("-i"):
inputfile = arg
if len(argv) == 1:
inputfile = argv[0]
if inputfile == '':
print instructions
sys.exit(2)
print author_information
setup_gimp()
process(inputfile)
if __name__ == "__main__":
main(sys.argv[1:])