-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcad2gis.py
executable file
·529 lines (453 loc) · 17.1 KB
/
cad2gis.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
#!/usr/bin/env python3
import logging
import fiona
import geopandas
import pandas
import shapely
import json
import os
import pathlib
import shutil
import subprocess
import sys
import traceback
import uuid
from flask import Flask, flash, request, redirect, url_for, send_from_directory
from flask_socketio import SocketIO, send, emit
from werkzeug.utils import secure_filename
import time
import math
logger = logging.getLogger('autocad-to-gis')
logger.setLevel(logging.DEBUG)
logging.basicConfig(filename='log/cad2gis.log', filemode='w')
# logger.info('start logger')
# #!/bin/bash
# set -e
# set -x
# rm -rf /tmp/.X99-lock
# Xvfb :99 -screen 0 1024x768x16 &
# while [ ! -e /tmp/.X11-unix/X99 ]; do sleep 0.1; done
# export DXF_ENCODING=UTF-8
# export XDG_RUNTIME_DIR='/tmp/runtime-root'
# for folder in $(find /data -type f -name "*.dwg" -exec dirname {} \; | sort -u); do
# DISPLAY=:99.0 ODAFileConverter \
# "${folder}" \
# "${folder}" \
# ACAD2018 DXF 1 1 "*.dwg"
# # ACAD2015 DXF 1 1 "*.dwg"
# # ACAD2000 DXF 1 1 "*.dwg"
# # ACAD2014 DXF 1 1 "*.dwg"
# # ACAD12 DXF 1 1 "*.dwg"
# # ACAD2010 DXF 1 1 "*.dwg"
# done
# echo "finished !"
UPLOAD_FOLDER = '/data'
ALLOWED_EXTENSIONS = {'dwg', 'dxf'}
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 40 * 1000 * 1000
socketio = SocketIO(
app,
logger=True,
engineio_logger=True,
cors_allowed_origins="*")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def allowed_setup(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in {'json'}
# wget http://localhost:9090/data/8fa47945beae4b56af301f0b9e0a3dcf.dwg
# @app.route('/api/cad2gis/data/<name>')
# def download_file(name):
# return send_from_directory(app.config["UPLOAD_FOLDER"], name)
@app.route('/api/cad2gis/get_geojson/<file_id>', methods=['GET'])
def get_geojson(file_id):
return send_from_directory(app.config["UPLOAD_FOLDER"], file_id, "geojson", "plan.geojson")
@app.route('/api/cad2gis/wait_geojson/<file_id>', methods=['GET'])
def wait_geojson(file_id):
file_path=os.path.join(app.config["UPLOAD_FOLDER"], file_id, "geojson", "plan.geojson")
status = 400
message = "isn't a file"
while not os.exists(file_path):
time.sleep(1)
if os.path.isfile(file_path):
status = 200
message = "file ready"
return app.response_class(
response=json.dumps(
{"message": message}
),
status=status,
mimetype='application/json'
)
# curl -F "file=@autocad-to-gis/data/kingconf/sett_22-11_14.dxf" \
# https://autocad-to-geojson.openindoor.io/api/cad2gis/convert/-0.8782/47.0545/0.0/0.0/45.0/0.5
@app.route('/api/cad2gis/convert/<float(signed=True):lng>/<float(signed=True):lat>/<float(signed=True):xoff>/<float(signed=True):yoff>/<float(signed=True):rot>/<float(signed=True):scale>', methods=['POST'])
@app.route('/api/cad2gis/convert/<float(signed=True):lng>/<float(signed=True):lat>/<float(signed=True):xoff>/<float(signed=True):yoff>/<float(signed=True):rot>', methods=['POST'])
@app.route('/api/cad2gis/convert/<float(signed=True):lng>/<float(signed=True):lat>/<float(signed=True):xoff>/<float(signed=True):yoff>', methods=['POST'])
@app.route('/api/cad2gis/convert/<float(signed=True):lng>/<float(signed=True):lat>', methods=['POST'])
def convert_all(lng, lat, xoff = 0.0, yoff = 0.0, rot = 0.0, scale = 1.0):
return convert_all_(lng, lat, xoff, yoff, rot, scale)
def convert_all_(lng, lat, xoff = 0.0, yoff = 0.0, rot = 0.0, scale = 1.0):
filename = "plan.dwg"
file_base = os.path.splitext(filename)[0]
file_ext = os.path.splitext(filename)[1]
file_uuid = uuid.uuid4().hex
# AutoCAD file
autocad_folder = os.path.join(
app.config['UPLOAD_FOLDER'],
file_uuid,
"autocad",
)
pathlib.Path(autocad_folder).mkdir(parents=True, exist_ok=True)
autocad_path = os.path.join(
autocad_folder,
filename
)
logger.debug('Original file: %s', autocad_path)
# ASCII DXF
ascii_dxf_folder = os.path.join(
app.config['UPLOAD_FOLDER'],
file_uuid,
"ascii_dxf",
)
pathlib.Path(ascii_dxf_folder).mkdir(parents=True, exist_ok=True)
ascii_dxf_path = os.path.join(
ascii_dxf_folder,
file_base + ".dxf"
)
logger.debug('DXF ASCII file: %s', ascii_dxf_path)
# geojson file
geojson_folder = os.path.join(
app.config['UPLOAD_FOLDER'],
file_uuid,
"geojson",
)
pathlib.Path(geojson_folder).mkdir(parents=True, exist_ok=True)
geojson_path = os.path.join(
geojson_folder,
file_base + ".geojson"
)
logger.debug('Geojson to write: %s', geojson_path)
with open(autocad_path, "bw") as f:
chunk_size = 4096
while True:
chunk = request.stream.read(chunk_size)
if len(chunk) == 0:
break
f.write(chunk)
# send("file received")
# file.save(autocad_path)
# Get setup as a file
setup = None
my_setup = {}
if 'setup' not in request.files:
logger.debug('No setup file')
# flash('No setup part')
else:
setup = request.files['setup']
if setup.filename == '':
logger.debug('No setup file')
if not (setup and allowed_setup(setup.filename)):
setup = None
if setup is not None:
logger.info('setup.filename: %s', setup.filename)
setup_path = os.path.join(
app.config['UPLOAD_FOLDER'],
file_uuid,
"setup.json",
)
setup.save(setup_path)
with open(setup_path, 'r') as f:
my_setup = json.load(f)
if "scale" in my_setup:
scale = my_setup['scale']
if "rotation" in my_setup:
rot = my_setup['rotation']
if "longitude" in my_setup:
lng = my_setup['longitude']
if "latitude" in my_setup:
lat = my_setup['latitude']
# Convert Autocad data to ASCII DXF
print('autocad_path:', autocad_path, file=sys.stderr, flush=True)
cmd = [
"/openindoor/autocad-to-ascii_dxf.sh",
autocad_folder,
ascii_dxf_folder,
]
print(cmd, file=sys.stderr, flush=True)
logger.info('cmd: %s', cmd)
print('Process AutoCAD to ASCII dxf conversion...', end='', file=sys.stderr, flush=True)
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
)
print('OK !', file=sys.stderr, flush=True)
# shell=True,
# capture_output=True,
# text=True,
# stdout=subprocess.PIPE,
# env={
# "DXF_ENCODING": "UTF-8",
# "XDG_RUNTIME_DIR": "/tmp/runtime-root",
# "DISPLAY": ":99.0",
# }
# )
ogrinfo = subprocess.run(
[
"ogrinfo",
"-ro",
ascii_dxf_path
],
stdout = subprocess.PIPE,
env={"DXF_FEATURE_LIMIT_PER_BLOCK": "-1"}
)
print('ogrinfo:', ogrinfo, file=sys.stderr, flush=True)
logger.info('ogrinfo: %s', ogrinfo)
# print(result.stderr, file=sys.stderr)
if (result.returncode != 0):
print("returncode:", result.returncode, file=sys.stderr, flush=True)
return app.response_class(
response=json.dumps(
{"returncode": result.returncode}
),
status=400,
mimetype='application/json'
)
# print(result.stderr, file=sys.stderr)
# Convert ASCII DXF file to geojson
# lambert_93 = "EPSG:27561"
# https://gdal.org/drivers/vector/dxf.html
# GDAL writes DXF files with measurement units set to “Imperial - Inches”. If you need to change the units, edit the $MEASUREMENT and $INSUNITS variables in the header template.
crs = "+proj=lcc" \
" +lat_1=" + str(lat) + "" \
" +lat_0=" + str(lat) + "" \
" +lon_0=" + str(lng) + "" \
" +k_0=0.999877341" \
" +a=6378249.2" \
" +b=6356515" \
" +units=m" \
" +no_defs"
# " +towgs84=-168,-60,320,0,0,0,0" \
# " +pm=paris" \
# " +x_0=600000" \
# " +y_0=200000" \
print("crs:", crs, file=sys.stderr)
# crs = '+proj=lcc +lat_1=' + str(lat) + ' +units=m +no_defs'
try:
# ascii_dxf = geopandas.read_file(
# ascii_dxf_path,
# encoding='utf-8',
# env = fiona.Env(DXF_ENCODING="UTF-8")
# )
print('Importing collection 1/2...', end='', file=sys.stderr, flush=True)
collection = list(fiona.open(ascii_dxf_path,'r'))
print('OK !', file=sys.stderr, flush=True)
print('Importing collection 2/2...', end='', file=sys.stderr, flush=True)
df1 = pandas.DataFrame(collection)
print('OK !', file=sys.stderr, flush=True)
#Check Geometry
def isvalid(geom):
try:
shapely.geometry.shape(geom)
return 1
except:
return 0
df1['isvalid'] = df1['geometry'].apply(lambda x: isvalid(x))
df1 = df1[df1['isvalid'] == 1]
collection = json.loads(df1.to_json(orient='records'))
#Convert to geodataframe
ascii_dxf = geopandas.GeoDataFrame.from_features(collection)
# logger.debug('ascii_dxf: %s', ascii_dxf)
print('ascii_dxf.crs:', ascii_dxf.crs, file=sys.stderr)
ascii_dxf.set_crs(
crs = crs,
inplace = True
)
print('ascii_dxf.crs:', ascii_dxf.crs, file=sys.stderr, flush=True)
logger.info('Applied crs: %s', ascii_dxf.crs)
# total_bounds = ascii_dxf.total_bounds
# print('total_bounds:', total_bounds, file=sys.stderr)
# print('total_bounds[0]:', total_bounds[0], file=sys.stderr)
print('calculate total bounds...', end='', file=sys.stderr, flush=True)
total_bounds = ascii_dxf.total_bounds
print('OK !', file=sys.stderr, flush=True)
# Add a bounding box
# bbox_geom = shapely.geometry.box(*total_bounds)
# # logger.debug('total_bounds arguments: %s', total_bounds)
# logger.debug('bbox geometry: %s', bbox_geom)
# my_bb = geopandas.GeoDataFrame({
# 'Layer': ['bounding_box'],
# 'geometry': [bbox_geom]
# })
# ascii_dxf = geopandas.GeoDataFrame( pandas.concat( (ascii_dxf, my_bb), ignore_index=True) )
print('Process rotation ' + str(rot) + ' ...', end='', file=sys.stderr, flush=True)
if (not math.isclose(rot, 0.0)):
ascii_dxf['geometry'] = ascii_dxf.rotate(
angle = rot,
origin=(
(total_bounds[2] + total_bounds[0]) / 2,
(total_bounds[3] + total_bounds[1]) / 2
)
)
print('Done !', file=sys.stderr, flush=True)
else:
print('Nothing to do !', file=sys.stderr, flush=True)
# logger.debug('ascii_dxf rotated: %s', ascii_dxf)
logger.debug('total_bounds: %s', total_bounds)
print('Process scaling ' + str(scale) + " ...", end='', file=sys.stderr, flush=True)
if (not math.isclose(scale, 1.0)):
ascii_dxf['geometry'] = ascii_dxf.scale(
xfact=scale,
yfact=scale,
zfact=scale,
origin=(
(total_bounds[2] + total_bounds[0]) / 2,
(total_bounds[3] + total_bounds[1]) / 2
)
)
print('Done !', file=sys.stderr, flush=True)
else:
print('Nothing to do !', file=sys.stderr, flush=True)
# logger.debug('ascii_dxf scaled: %s', ascii_dxf)
# print('total_bounds 2:', total_bounds, file=sys.stderr)
print('Process EPSG:4326...', end='', file=sys.stderr, flush=True)
my_epsg = ascii_dxf.to_crs('EPSG:4326')
print('OK !', file=sys.stderr, flush=True)
# logger.debug('my_epsg to_crs: %s', my_epsg)
# my_lcc_geojson = ascii_dxf.to_json()
# total_bounds = my_epsg.total_bounds
print('Process translation (' + str(xoff) + ', ' + str(yoff) + ', 0.0) ...', end='', file=sys.stderr, flush=True)
logger.info('Apply offsets: xoff:%s yoff:%s', xoff, yoff)
if not (math.isclose(xoff, 0.0) and math.isclose(yoff, 0.0)):
my_epsg['geometry'] = my_epsg.translate(
xoff=xoff,
yoff=yoff,
zoff=0.0
)
print('Done !', file=sys.stderr, flush=True)
else:
print('Nothing to do !', file=sys.stderr, flush=True)
# Start file_content twist
if ("transforms" in my_setup):
for transform in my_setup["transforms"]:
logger.debug('transform: %s', transform)
if ("rules" in transform):
my_sub_epsg = my_epsg
for rule in transform["rules"]:
logger.debug('rule: %s', rule)
value = transform["rules"][rule]
logger.debug('value: %s', value)
my_sub_epsg = my_sub_epsg[my_sub_epsg[rule]==value]
logger.debug('my_sub_epsg: %s', my_sub_epsg)
my_sub_epsg = my_sub_epsg.apply(
lambda row: alter_row(row, transform),
axis=1,
result_type='expand'
)
my_epsg = my_epsg.merge(my_sub_epsg, how='left')
print('Convert to geojson...', end='', file=sys.stderr, flush=True)
my_geojson = my_epsg.to_json(na = 'drop')
final_geojson = json.loads(my_geojson)
final_geojson["@context"] = {
"id": file_uuid,
}
print('OK !', file=sys.stderr, flush=True)
print('geojson_path:', geojson_path, file=sys.stderr)
logger.info('geojson generated here: %s', geojson_path)
my_geojson = json.dumps(final_geojson)
# my_epsg.to_file(geojson_path, driver='GeoJSON')
with open(geojson_path, 'w') as f:
f.write(my_geojson)
# print("geojson:", my_geojson, file=sys.stderr)
except Exception:
print(traceback.format_exc(), file=sys.stderr)
# cmd = [
# "ogr2ogr", "-f", "GeoJSON",
# # "/vsistdin/",
# "/vsistdout/",
# # geojson_path,
# ascii_dxf_path,
# ]
# print("cmd:", cmd, file=sys.stderr)
# my_exec = subprocess.run(
# cmd,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
# env={
# "DXF_ENCODING": "UTF-8",
# }
# )
# print(my_exec.stdout, file=sys.stderr)
# print(my_exec.stderr, file=sys.stderr)
# my_geojson = json.loads(my_exec.stdout)
print('Return result !', end='', file=sys.stderr, flush=True)
return app.response_class(
response=my_geojson,
status=200,
mimetype='application/json'
)
# print('calculate_content_length():', response.calculate_content_length(), file=sys.stderr, flush=True)
# return response
def alter_row(row, transform):
if ("properties" in transform):
for property in transform["properties"]:
value = transform["properties"][property]
if value.startswith('$'):
value = row[value[1:]]
# logger.debug('value: %s', value)
row[property] = value
# logger.debug('row: %s', row)
return row
# # wget http://localhost:9090/data/8fa47945beae4b56af301f0b9e0a3dcf.dwg
# @app.route('/data/<name>')
# def download_file(name):
# return send_from_directory(app.config["UPLOAD_FOLDER"], name)
app.add_url_rule(
"/uploads/<name>", endpoint="download_file", build_only=True
)
# curl http://localhost:9090/api/dwg-to-dxf/8fa47945beae4b56af301f0b9e0a3dcf
@app.route("/api/dwg-to-dxf/<string:file_id>")
def dwg_to_dxf(file_id):
dest_dir = "/tmp/dwg_" + file_id
tmp_dwg = dest_dir + "/my.dwg"
pathlib.Path(dest_dir).mkdir(parents=True, exist_ok=True)
shutil.copy2("/data/" + file_id + ".dwg", tmp_dwg)
# DISPLAY=:99.0 ODAFileConverter \
# /tmp/dwg_8fa47945beae4b56af301f0b9e0a3dcf \
# /tmp/dwg_8fa47945beae4b56af301f0b9e0a3dcf \
# ACAD2018 DXF 1 1 "*.dwg"
cmd = [
"ODAFileConverter",
dest_dir,
dest_dir,
"ACAD2018", "DXF", "1", "1", '"*.dwg"'
]
logger.info('cmd: %s', cmd)
cmd = ["env"]
subprocess.run(
cmd,
shell=True,
capture_output=True,
text=True,
)
response = app.response_class(
status=200,
)
return response
@socketio.on('autocad_file')
def on_autocad_file(data):
print('------------- coucou', file=sys.stderr, flush=True)
os._exit(1)
# @socketio.on('my event')
# def handle_my_custom_event(json):
# print('received json: ' + str(json), file=sys.stderr, flush=True)
@socketio.on('my event')
def handle_my_custom_event(json):
print('------------- received json: ' + str(json), file=sys.stderr, flush=True)
if __name__ == '__main__':
# app.run(port=5000, debug=True, host="0.0.0.0")
socketio.run(app, port=5000, debug=True, host="0.0.0.0")