Skip to main content
added 138 characters in body
Source Link
Pieter
  • 4.6k
  • 1
  • 11
  • 26

You canThe typical way to "group by" a GeoDataFrame is to use dissolve to. You group on a columnby one or more columns, and create a union of the geometry column of the resulting GeoDataFrame will contain a union of all grouped values.

Next, you can convert the unioned points (or rather multipoints) to linestrings.

Code sample:

import geopandas as gpd
import shapely
from shapely import Point

input = gpd.GeoDataFrame(
    {
        "userid": [1, 2, 2, 3, 3], 
        "geometry": [
            Point(0,0), Point(1,0), Point(1,1), Point(2,0), Point(2,1)
        ],
    },
    crs="EPSG:31370",
)

diss = input.dissolve(by="userid")
diss_multi = diss.loc[diss.geometry.type == "MultiPoint"]

print(diss)

lines = diss_multi.set_geometry(
    diss_multi.geometry.apply(lambda x: shapely.LineString(shapely.get_coordinates(x)))
)

print(lines)

Result:

userid
2       LINESTRING (1 0, 1 1)
3       LINESTRING (2 0, 2 1)

PS: it is always easier if you include a runnable minimal reproducible example in code including input data. Based on how I understood your question I tried to create "runnable" sample data in the code sample.

You can use dissolve to group on a column and create a union of the geometry column.

Next, you can convert the unioned points to linestrings.

Code sample:

import geopandas as gpd
import shapely
from shapely import Point

input = gpd.GeoDataFrame(
    {
        "userid": [1, 2, 2, 3, 3], 
        "geometry": [
            Point(0,0), Point(1,0), Point(1,1), Point(2,0), Point(2,1)
        ],
    },
    crs="EPSG:31370",
)

diss = input.dissolve(by="userid")
diss_multi = diss.loc[diss.geometry.type == "MultiPoint"]

print(diss)

lines = diss_multi.set_geometry(
    diss_multi.geometry.apply(lambda x: shapely.LineString(shapely.get_coordinates(x)))
)

print(lines)

Result:

userid
2       LINESTRING (1 0, 1 1)
3       LINESTRING (2 0, 2 1)

PS: it is always easier if you include a runnable minimal reproducible example in code including input data. Based on how I understood your question I tried to create "runnable" sample data in the code sample.

The typical way to "group by" a GeoDataFrame is to use dissolve. You group by one or more columns, and the geometry column of the resulting GeoDataFrame will contain a union of all grouped values.

Next, you can convert the unioned points (or rather multipoints) to linestrings.

Code sample:

import geopandas as gpd
import shapely
from shapely import Point

input = gpd.GeoDataFrame(
    {
        "userid": [1, 2, 2, 3, 3], 
        "geometry": [
            Point(0,0), Point(1,0), Point(1,1), Point(2,0), Point(2,1)
        ],
    },
    crs="EPSG:31370",
)

diss = input.dissolve(by="userid")
diss_multi = diss.loc[diss.geometry.type == "MultiPoint"]

print(diss)

lines = diss_multi.set_geometry(
    diss_multi.geometry.apply(lambda x: shapely.LineString(shapely.get_coordinates(x)))
)

print(lines)

Result:

userid
2       LINESTRING (1 0, 1 1)
3       LINESTRING (2 0, 2 1)

PS: it is always easier if you include a runnable minimal reproducible example in code including input data. Based on how I understood your question I tried to create "runnable" sample data in the code sample.

Source Link
Pieter
  • 4.6k
  • 1
  • 11
  • 26

You can use dissolve to group on a column and create a union of the geometry column.

Next, you can convert the unioned points to linestrings.

Code sample:

import geopandas as gpd
import shapely
from shapely import Point

input = gpd.GeoDataFrame(
    {
        "userid": [1, 2, 2, 3, 3], 
        "geometry": [
            Point(0,0), Point(1,0), Point(1,1), Point(2,0), Point(2,1)
        ],
    },
    crs="EPSG:31370",
)

diss = input.dissolve(by="userid")
diss_multi = diss.loc[diss.geometry.type == "MultiPoint"]

print(diss)

lines = diss_multi.set_geometry(
    diss_multi.geometry.apply(lambda x: shapely.LineString(shapely.get_coordinates(x)))
)

print(lines)

Result:

userid
2       LINESTRING (1 0, 1 1)
3       LINESTRING (2 0, 2 1)

PS: it is always easier if you include a runnable minimal reproducible example in code including input data. Based on how I understood your question I tried to create "runnable" sample data in the code sample.