# Spatial Algorithms

 Pay Notebook Creator: Roy Hyunjin Han 0 Set Container: Numerical CPU with TINY Memory for 10 Minutes 0 Total 0
In :
# CrossCompute
geometries_text_path = 'geometries.txt'
target_folder = '/tmp'

In :
from shapely import wkt
from shapely.geometry import GeometryCollection

geometries = []
for line in open(geometries_text_path):
GeometryCollection(geometries)

Out:
<shapely.geometry.collection.GeometryCollection at 0x7efd0c3edd30>
In :
from shapely.geometry import Point

def sum_distances(xy):
return sum(Point(xy).distance(g) for g in geometries)

sum_distances((0, 0))

Out:
2.8284271247461903
In :
from numpy.random import rand
from scipy.optimize import minimize
initial_xy = rand(2)
r = minimize(sum_distances, initial_xy, method='L-BFGS-B')
r

Out:
      fun: 2.732050807605542
hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>
jac: array([ -5.19584376e-06,   9.10382880e-06])
nfev: 21
nit: 5
status: 0
success: True
x: array([ -1.85893340e-06,   4.22656731e-01])
In :
store_point = Point(r.x)
print(store_point)

POINT (-1.858933395716981e-06 0.4226567310577499)

In :
from geotable import ColorfulGeometryCollection

geometry_collection = ColorfulGeometryCollection([
GeometryCollection(geometries),
store_point,
], colors=['blue', 'yellow'])
geometry_collection

Out:
<geotable.ColorfulGeometryCollection at 0x7efceb06ef98>
In :
from cairosvg import svg2png
from os.path import join

target_path = join(target_folder, 'geometries.png')
svg2png(
bytestring=geometry_collection._repr_svg_(),
write_to=target_path)
print('geometry_image_path = %s' % target_path)

geometry_image_path = /tmp/geometries.png

In :
print('store_x = %s' % store_point.x)
print('store_y = %s' % store_point.y)

store_x = -1.8589333957169814e-06
store_y = 0.4226567310577499