With all your solutions, you try to create a LineString with only one point
Since all the dates (hours) are different:
geo_df = geo_df.groupby(['date_time'])
print("number of groups:", geo_df.ngroups)
number of groups: 5 # and 5 original geometries
for group in gdf.groupby(['date_time']).geometry:
print(group)
(Timestamp('2019-11-08 08:21:38'), 0 POINT (-2.466583 36.830444)
Name: geometry, dtype: object)
(Timestamp('2019-11-08 08:32:15'), 1 POINT (-2.466611 36.83025)
Name: geometry, dtype: object)
(Timestamp('2019-11-08 08:41:51'), 2 POINT (-2.466778 36.830417)
Name: geometry, dtype: object)
(Timestamp('2019-11-08 08:51:53'), 3 POINT (-2.466833 36.830361)
Name: geometry, dtype: object)
(Timestamp('2019-11-08 09:01:50'), 4 POINT (-2.466806 36.830528)
Name: geometry, dtype: object)
Only one point per group -> Error: ValueError: LineStrings must have at least 2 coordinate tuples)
And
geometry = [xy for xy in zip(df.longitude, df.latitude)]
print(geometry)
[(-2.466583, 36.830444), (-2.466611, 36.83025), (-2.4667779999999997, 36.830417), (-2.4668330000000003, 36.830360999999996), (-2.466806, 36.830528)
# geometry is a list of point coordinates
Point(geometry[0]).wkt
'POINT (-2.466583 36.830444)'
#but
LineString(geometry[0])
...
ValueError: Input (-2.466583, 36.830444) is the wrong shape for a LineString
And I suppose you want to create a line with all the points, therefore
print(LineString(geometry).wkt)
LINESTRING (-2.466583 36.830444, -2.466611 36.83025, -2.466778 36.830417, -2.466833 36.830361, -2.466806 36.830528)
or directly
print(LineString(df.geometry).wkt)
LINESTRING (-2.466583 36.830444, -2.466611 36.83025, -2.466778 36.830417, -2.466833 36.830361, -2.466806 36.830528)
If you want to use groupby to create separate lines choose another column to regroup (look at Turning GeoDataFrame of x,y coordinates into Linestrings using GROUPBY? or Convert Points to Lines Geopandas)
with data02 modified (2 groups, 0 and 1)
date_time,latitude,longitude,year,data01,hour,minute,data02,wtkcolumn
2019-11-08 08:21:38,36.830444,-2.466583, 2019, 312, 8 , 21 , 0 , POINT (-2.466583 36.830444)
2019-11-08 08:32:15,36.830250,-2.466611, 2019, 312, 8 , 32, 0, POINT (-2.466611 36.83025)
2019-11-08 08:41:51 ,36.830417,-2.466778, 2019, 312, 8 , 41 , 1 , POINT (-2.466778 36.830417)
2019-11-08 08:51:53 ,36.830361,-2.466833, 2019, 312, 8, 51, 1 , POINT (-2.466833 36.830361)
2019-11-08 09:01:50, 36.830528, -2.466806, 2019, 312, 9 , 1 , 1, POINT (-2.466806 36.830528)
list(geo_df.groupby(['data02'])['geometry'].apply(lambda x: LineString(x.tolist())))
[<shapely.geometry.linestring.LineString object at 0x11bc81668>, <shapely.geometry.linestring.LineString object at 0x11bc81400>]
If I wanted to generate a set of points spaced equally within the polygon, here's how I would do it using shapely (based off of this answer):
import numpy as np
from shapely.geometry import Point, Polygon
def get_evenly_spaced_points_in_polygon(poly, spacing):
"""Get a list of (lat, long) pairs spaced evenly through poly."""
points = []
minx, miny, maxx, maxy = poly.bounds
for x in np.arange(minx, maxx, spacing):
for y in np.arange(miny, maxy, spacing):
p = Point(x, y)
points.append((p.y, p.x) if poly.contains(p))
return points
lons = [run.gdt1[1], run.gdt2[1], run.target_area[1]]
lats = [run.gdt1[0], run.gdt2[0], run.target_area[0]]
poly = Polygon(list(zip(lons, lats)))
spacing = 0.1 # set to whatever you want the spacing to be between points
points = get_evenly_spaced_points_in_polygon(poly, spacing)
For example, if you have three points at long/lat of (1, 5)
, (2, 8)
, and (1, 8)
and use spacing=0.1
:
lons = [1, 2, 1]
lats = [5, 8, 8]
poly = Polygon(list(zip(lons, lats)))
points = get_evenly_spaced_points_in_polygon(poly, spacing=0.1)
print(points)
gives
[(5.399999999999999, 1.1), (5.499999999999998, 1.1), (5.599999999999998, 1.1), (5.6999999999999975, 1.1), (5.799999999999997, 1.1), (5.899999999999997, 1.1), (5.9999999999999964, 1.1), (6.099999999999996, 1.1), (6.199999999999996, 1.1), (6.299999999999995, 1.1), (6.399999999999995, 1.1), (6.499999999999995, 1.1), (6.599999999999994, 1.1), (6.699999999999994, 1.1), (6.799999999999994, 1.1), (6.899999999999993, 1.1), (6.999999999999993, 1.1), (7.0999999999999925, 1.1), (7.199999999999992, 1.1), (7.299999999999992, 1.1), (7.3999999999999915, 1.1), (7.499999999999991, 1.1), (7.599999999999991, 1.1), (7.69999999999999, 1.1), (7.79999999999999, 1.1), (7.89999999999999, 1.1), (5.6999999999999975, 1.2000000000000002), (5.799999999999997, 1.2000000000000002), (5.899999999999997, 1.2000000000000002), (5.9999999999999964, 1.2000000000000002), (6.099999999999996, 1.2000000000000002), (6.199999999999996, 1.2000000000000002), (6.299999999999995, 1.2000000000000002), (6.399999999999995, 1.2000000000000002), (6.499999999999995, 1.2000000000000002), (6.599999999999994, 1.2000000000000002), (6.699999999999994, 1.2000000000000002), (6.799999999999994, 1.2000000000000002), (6.899999999999993, 1.2000000000000002), (6.999999999999993, 1.2000000000000002), (7.0999999999999925, 1.2000000000000002), (7.199999999999992, 1.2000000000000002), (7.299999999999992, 1.2000000000000002), (7.3999999999999915, 1.2000000000000002), (7.499999999999991, 1.2000000000000002), (7.599999999999991, 1.2000000000000002), (7.69999999999999, 1.2000000000000002), (7.79999999999999, 1.2000000000000002), (7.89999999999999, 1.2000000000000002), (5.9999999999999964, 1.3000000000000003), (6.099999999999996, 1.3000000000000003), (6.199999999999996, 1.3000000000000003), (6.299999999999995, 1.3000000000000003), (6.399999999999995, 1.3000000000000003), (6.499999999999995, 1.3000000000000003), (6.599999999999994, 1.3000000000000003), (6.699999999999994, 1.3000000000000003), (6.799999999999994, 1.3000000000000003), (6.899999999999993, 1.3000000000000003), (6.999999999999993, 1.3000000000000003), (7.0999999999999925, 1.3000000000000003), (7.199999999999992, 1.3000000000000003), (7.299999999999992, 1.3000000000000003), (7.3999999999999915, 1.3000000000000003), (7.499999999999991, 1.3000000000000003), (7.599999999999991, 1.3000000000000003), (7.69999999999999, 1.3000000000000003), (7.79999999999999, 1.3000000000000003), (7.89999999999999, 1.3000000000000003), (6.299999999999995, 1.4000000000000004), (6.399999999999995, 1.4000000000000004), (6.499999999999995, 1.4000000000000004), (6.599999999999994, 1.4000000000000004), (6.699999999999994, 1.4000000000000004), (6.799999999999994, 1.4000000000000004), (6.899999999999993, 1.4000000000000004), (6.999999999999993, 1.4000000000000004), (7.0999999999999925, 1.4000000000000004), (7.199999999999992, 1.4000000000000004), (7.299999999999992, 1.4000000000000004), (7.3999999999999915, 1.4000000000000004), (7.499999999999991, 1.4000000000000004), (7.599999999999991, 1.4000000000000004), (7.69999999999999, 1.4000000000000004), (7.79999999999999, 1.4000000000000004), (7.89999999999999, 1.4000000000000004), (6.599999999999994, 1.5000000000000004), (6.699999999999994, 1.5000000000000004), (6.799999999999994, 1.5000000000000004), (6.899999999999993, 1.5000000000000004), (6.999999999999993, 1.5000000000000004), (7.0999999999999925, 1.5000000000000004), (7.199999999999992, 1.5000000000000004), (7.299999999999992, 1.5000000000000004), (7.3999999999999915, 1.5000000000000004), (7.499999999999991, 1.5000000000000004), (7.599999999999991, 1.5000000000000004), (7.69999999999999, 1.5000000000000004), (7.79999999999999, 1.5000000000000004), (7.89999999999999, 1.5000000000000004), (6.899999999999993, 1.6000000000000005), (6.999999999999993, 1.6000000000000005), (7.0999999999999925, 1.6000000000000005), (7.199999999999992, 1.6000000000000005), (7.299999999999992, 1.6000000000000005), (7.3999999999999915, 1.6000000000000005), (7.499999999999991, 1.6000000000000005), (7.599999999999991, 1.6000000000000005), (7.69999999999999, 1.6000000000000005), (7.79999999999999, 1.6000000000000005), (7.89999999999999, 1.6000000000000005), (7.199999999999992, 1.7000000000000006), (7.299999999999992, 1.7000000000000006), (7.3999999999999915, 1.7000000000000006), (7.499999999999991, 1.7000000000000006), (7.599999999999991, 1.7000000000000006), (7.69999999999999, 1.7000000000000006), (7.79999999999999, 1.7000000000000006), (7.89999999999999, 1.7000000000000006), (7.499999999999991, 1.8000000000000007), (7.599999999999991, 1.8000000000000007), (7.69999999999999, 1.8000000000000007), (7.79999999999999, 1.8000000000000007), (7.89999999999999, 1.8000000000000007), (7.79999999999999, 1.9000000000000008), (7.89999999999999, 1.9000000000000008)]
(if you wanted, you could use numpy.round_ to prevent the annoying floating point stuff)
Best Answer
You can filter your GeoDataFrame based on specific values in the columns.
In this case you can filter your GeoDataFrame to contain only points with an Angle of Point between 50 and 150. You can also do this for any other columns and values.
If you want, you can then extract the list of points that have this point of angle if you want to append them to some other list.