Spatial Algorithms




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Define Geometries from WKT

Well Known Text is a standard text-based format for defining geometries.

  • Specify spatial coordinates with the first number as longitude and the second number as latitude.

    POINT (-73.935242 40.730610)  # LONGITUDE=-73.935242 LATITUDE=40.730610
  • Note that polygons must be closed, meaning that the first and last coordinates must be the same.

    POLYGON ((0 0, 0 1, 1 1))       # BAD
    POLYGON ((0 0, 0 1, 1 1, 0 0))  # GOOD
  • Add holes to polygons by specifying a second set of coordinates.

    POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (1 1, 1 9, 9 9, 9 1, 1 1))

{ wkt_text : Geometries in WKT format ? Specify each geometry on a separate line }

In [1]:
# CrossCompute
wkt_text_path = 'geometries.txt'
target_folder = '/tmp'
In [2]:
from shapely.geometry import GeometryCollection
from shapely import wkt

geometries = []
for line in open(wkt_text_path):
    geometry = wkt.loads(line)
    print(geometry)
    geometries.append(geometry)
GeometryCollection(geometries)
POINT (3 7)
POINT (7 7)
LINESTRING (5 6, 4 5, 5 5)
POLYGON ((2 2, 2 4, 8 4, 8 2, 2 2))
POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (1 1, 1 9, 9 9, 9 1, 1 1))
Out[2]:
<shapely.geometry.collection.GeometryCollection at 0x7f5eaca9a2e8>
In [3]:
from geotable import ColorfulGeometryCollection
ColorfulGeometryCollection(geometries)
Out[3]:
<geotable.ColorfulGeometryCollection at 0x7f5eaca67d68>
In [4]:
from cairosvg import svg2png
from os.path import join

target_path = join(target_folder, 'geometries.png')
svg_html = ColorfulGeometryCollection(geometries)._repr_svg_()
svg2png(bytestring=svg_html, write_to=target_path)
print('geometries_image_path = %s' % target_path)
geometries_image_path = /tmp/geometries.png

Rendered Geometries

{ geometries_image : Rendered Geometries }