Instantly share code, notes, and snippets.
Created
January 24, 2018 20:25
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save johanvdw/42e7ff581d45d5ab866a71392fb4729e to your computer and use it in GitHub Desktop.
Script to import c3nav data (33c3 to 34c3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import re | |
from c3nav.mapdata.models import * | |
from shapely.geometry import shape | |
from decimal import Decimal | |
levels={ | |
"0":Level.objects.filter(base_altitude=0)[0], | |
"0-1":Level.objects.filter(base_altitude=Decimal('0.10'))[0], | |
"1":Level.objects.filter(base_altitude=5)[0], | |
"1-2":Level.objects.filter(base_altitude=Decimal('5.10'))[0], | |
"2":Level.objects.filter(base_altitude=10)[0], | |
"2-3":Level.objects.filter(base_altitude=Decimal('10.10'))[0], | |
"3":Level.objects.filter(base_altitude=15)[0] | |
} | |
path = "/root/maps/buildings" | |
#for fn in os.listdir(path): | |
# with open(os.path.join(path,fn)) as f: | |
# data = json.load(f) | |
# geom = shape(data["geometry"]) | |
# Building.objects.create(geometry=geom, level=levels[data["level"]]) | |
# remove all spaces | |
#for s in Space.objects.all(): | |
# s.delete() | |
path = "/root/maps/rooms" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
if re.match("[0-9a-f]{13}", name): | |
Space.objects.create(geometry=geom, level=level, slug=name) | |
else: | |
Space.objects.create(geometry=geom, level=level, slug=name, | |
titles={"en":name}) | |
path = "/root/maps/outsides" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
if re.match("[0-9a-f]{13}", name): | |
Space.objects.create(geometry=geom, level=level, slug=name, outside=True) | |
else: | |
Space.objects.create(geometry=geom, level=level, slug=name, outside=True, | |
titles={"en":name}) | |
path = "/root/maps/doors" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
Door.objects.create(geometry=geom, level=level) | |
path = "/root/maps/stairs" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
# check with which space it overlaps and assign the id | |
for s in Space.objects.filter(level=level): | |
if s.geometry.intersects(geom): | |
space = s | |
Stair.objects.create(geometry=geom, space=space) | |
path = "/root/maps/obstacles" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
# check with which space it overlaps and assign the id | |
for s in Space.objects.filter(level=level): | |
if s.geometry.intersects(geom): | |
space = s | |
Obstacle.objects.create(geometry=geom, space=space) | |
path = "/root/maps/lineobstacles" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
# check with which space it overlaps and assign the id | |
for s in Space.objects.filter(level=level): | |
if s.geometry.intersects(geom): | |
space = s | |
LineObstacle.objects.create(geometry=geom, space=space) | |
# import booths | |
path = "/root/maps/arealocations" | |
for fn in os.listdir(path): | |
with open(os.path.join(path,fn)) as f: | |
data = json.load(f) | |
if data["location_type"] == "poi": | |
geom = shape(data["geometry"]) | |
name = os.path.splitext(fn)[0] | |
level = levels[data["level"]] | |
name = os.path.splitext(fn)[0] | |
# check with which space it overlaps and assign the id | |
for s in Space.objects.filter(level=level): | |
if s.geometry.intersects(geom): | |
space = s | |
Area.objects.create(geometry=geom, space=space, titles=data["titles"], slug=name) | |
# Give all these area the stands location group (should be created first) | |
# this does not work ... | |
stands = LocationGroup.objects.filter(slug="stands")[0] | |
for a in Area.objects.all(): | |
LocationGroup.objects.create(area=a,location_group=stands) | |
# for centroid-like: we can use shape.representative_point() | |
# one should run processupdates after this script | |
# remove all spaces | |
#for s in Space.objects.all(): | |
# s.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment