Izak Joubert
Izak Joubert

Reputation: 1001

How to extract coordinates of a geometric object without array in Shapely

How to extract only the value from an array without the text "array" and "typecode"?

Array is shapely.linestring.centroid.xy:

a = LineString.centroid.xy
print(a)
>> (array('d', [-1.72937...45182697]), array('d', [2.144161...64685937]))
print(a[0])
>> array('d', [-1.7293720645182697])

I need only the -1.7293... as a float not the whole array business.

Upvotes: 4

Views: 3682

Answers (2)

Georgy
Georgy

Reputation: 13697

In fact, individual coordinates of the Point can be accessed by x and y properties. And since object.centroid returns a Point, you can simply do:

>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (2, 1)])
>>> line.centroid.x
1.0
>>> line.centroid.y
0.5

Additionaly, geometric objects such as Point, LinearRing and LineString have a coords attribute that returns a special CoordinateSequence object from which you can get individual coordinates:

>>> line.coords
<shapely.coords.CoordinateSequence at 0x7f60e1556390>
>>> list(line.coords)
[(0.0, 0.0), (2.0, 1.0)]
>>> line.centroid.coords[0]
(1.0, 0.5)

Upvotes: 6

Jainil Patel
Jainil Patel

Reputation: 1324

print(a[0][0])

you are working with array inside array.

enter image description here

import array
a=(array.array('d',[-2.2,3,2,2]),array('d',[2,3,4]))
print(a[0][0])

Upvotes: 1

Related Questions