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.
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)
print(a[0][0])
you are working with array inside array.
import array
a=(array.array('d',[-2.2,3,2,2]),array('d',[2,3,4]))
print(a[0][0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With