7

Reading the documentation...it looks like I should have this right, but I don't.

The example is:

Point(0,0).distance(Point(1,1))

1.4142135623730951

Based on this example I wrote this block (..note: update, 'capital' starts as a shapely obj, and city_pt is a dict...full of multipoints it appears, looking like this).

[{'geometry': {'type': 'MultiPoint', 'coordinates': [(4942585.391221348, #3940520.723517349)]}, 'type': 'Feature', 'id': '17', 'properties': OrderedDict([(u'Status', u'Bad')])}]

    # capital = shapely obj
    capital_pt = capital.coords
    # city_point is a dict ... multipoints
    for city_point in filtered_all:
        city_items = shape(city_point['geometry'])
        # capital_pt = POINT (13531245.47570414 2886003.268927813) 
        # city_items[0] = POINT (4942585.391221348 3940520.723517349)

        # measure distance

        # calculate the distance between capital and cities
        distance_between_pts = capital_pt.distance(city_point)

and got this:

AttributeError: 'CoordinateSequence' object has no attribute 'distance'

trying the om_henners answer I get

AttributeError: 'dict' object has no attribute 'coords'

Just in terminal I tried this:

x = POINT (13531245.47570414 2886003.268927813).distance(POINT (4942585.391221348 3940520.723517349))

File "", line 1 x = POINT (13531245.47570414 2886003.268927813).distance(POINT (4942585.391221348 3940520.723517349)) ^ SyntaxError: invalid syntax

2 Answers 2

5

Your capital_pt is the coords attribute of the original capital shapely geometry object. In itself this is not a shapely geometry, rather a sequence of tuples of flots which are the point objects. Instead you should be using

distance_between_pts = capital.distance(city_items)

Given your case where you're trying to calculate distances for all points in a dataset opened by fiona, a quick example might help also:

import fiona
import shapely.geometry

# capital is a shapely geometry object

with fiona.open(path_to_data) as src:
    for feature in src:
        geom = shapely.geometry.shape(feature["geometry"])
        distance_between_pts = capital.distance(geom)
        print(distance_between_pts)
8
  • city_point is a dict. So that's basically: <class 'shapely.geometry.point.Point'> # capital <type 'dict'> # city_point
    – user14696
    Mar 24, 2015 at 5:24
  • Could this be a problem?...it appears I have MultiPoints (and capital is a Point). [{'geometry': {'type': 'MultiPoint', 'coordinates': [(4942585.391221348, 3940520.723517349)]}, 'type': 'Feature', 'id': '17', 'properties': OrderedDict([(u'Status', u'Bad')])}]
    – user14696
    Mar 24, 2015 at 5:27
  • @user14696 Updated my answer to reference city_items instead of city_point. Basically you want to use distance between two shapely geometries. And no, since there's only a single point in your MultiPoint geometry it should work fine for distance calculations.
    – om_henners
    Mar 24, 2015 at 5:31
  • good to know the multipoint is okay...but still running into road blocks. AttributeError: 'dict' object has no attribute 'coords'. distance_between_pts = capital.distance(city_items.coords) gives me the ' "Multi-part geometries do not provide a coordinate sequence") NotImplementedError: Multi-part geometries do not provide a coordinate sequence'.
    – user14696
    Mar 24, 2015 at 5:34
  • It should just be between the two shapely geometries, i.e. distance_between_pts = capital.distance(city_items) where both capital and city_items are shapely geometry objects. Ignore the coords attribute :)
    – om_henners
    Mar 24, 2015 at 5:39
1

POINT (13531245.47570414 2886003.268927813) is not a Shapely object but the WKT format of a Point

A point in Shapely is :

point1 =  Point(13531245.47570414,2886003.268927813)
type(point1)
<class 'shapely.geometry.point.Point'>

The WKT representation of the point is:

# WKT
print point1.wkt
POINT (13531245.47570414 2886003.268927813)
type(point1.wkt)
<type 'str'>

So, the correct formulation is Shapely_geometry.dist(Shapely_geometry) and not str.distance(str):

x = Point(13531245.47570414,2886003.268927813).distance(Point(4942585.391221348,3940520.723517349))
print x
8653154.86449

If you want to use the coordinates from the result of Fiona (without Shapely, your question How to call 'shapely' .coords on a fiona filtered list?)

import math # or numpy
def euclidean_distance( l1,l2):
    return math.sqrt((l2[0]-l1[0])**2 + (l2[1]-l1[1])**2)
 print euclidean_distance((13531245.47570414,2886003.268927813),(4942585.391221348,3940520.723517349))   
8653154.864488555

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.