Skip to content

Instantly share code, notes, and snippets.

@johanvdw
Last active January 13, 2017 23:14
Show Gist options
  • Save johanvdw/cc1ccc255a37e1d106fa1ef8703bf49a to your computer and use it in GitHub Desktop.
Save johanvdw/cc1ccc255a37e1d106fa1ef8703bf49a to your computer and use it in GitHub Desktop.
worldfile2bbox.py
#!/usr/bin/env python
# Small script to convert a worldfile from an image to a bounding box
# json which can be consumed by c3nav (https://github.com/c3nav/c3nav )
# Conversion of worldfile to bounding box based on
# http://gis.stackexchange.com/questions/120659/converting-esri-world-file-jgw-into-xy-corner-coordinates (but corrected)
#
# To create a worldfile for an image, you can use the "Georeferencer GDAL"
# plugin from QGis. This requires you to choose 3 or more control points.
# In the plugin settings choose "use worldfile, linear transformation only".
# Prior to doing so you can load the existing buildings/rooms/... as reference
# layer. You can just drag and drop all .json files on qgis.
import sys
from PIL import Image
import json
import os
#usage script worldfile image
ImageName = sys.argv[1]
WorldFileName = os.path.splitext(ImageName)[0]+'.wld'
JSONFileName = ImageName + '.json'
Cols, Rows = Image.open(open(ImageName)).size
with open(WorldFileName,'r') as WorldFile:
XCellSize = float(WorldFile.readline())
dont_care = WorldFile.readline() # should be 0 - rotation is not supported
dont_care = WorldFile.readline() # should be 0
YCellSize = float(WorldFile.readline())
WorldX = float(WorldFile.readline())
WorldY = float(WorldFile.readline())
XMin = WorldX - (XCellSize / 2)
YMax = WorldY + (YCellSize / 2)
XMax = (WorldX + (Cols * XCellSize)) + (XCellSize / 2)
YMin = (WorldY + (Rows * YCellSize)) - (YCellSize / 2)
data = {"bounds":[[YMin, XMin],[YMax, XMax]]}
with open(JSONFileName, 'w') as JSONFile:
json.dump(data, JSONFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment