Skip to content

Instantly share code, notes, and snippets.

@moosetraveller
Last active October 14, 2021 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moosetraveller/1d59d334188069f7601c3053864f6f84 to your computer and use it in GitHub Desktop.
Save moosetraveller/1d59d334188069f7601c3053864f6f84 to your computer and use it in GitHub Desktop.
Create Lines from a CSV File

Environment

Using Miniconda (without ArcGIS Pro)

  • Download and install Miniconda3 (https://docs.conda.io/en/latest/miniconda.html)
  • Create a new Conda environment with conda create --name gis
  • Activate Conda environment with conda activate gis
  • Install required Python packages with conda install shapely pandas geopandas
  • Use this Conda environment in your development environment

Using ArcGIS Pro Conda Environment

  1. Open ArcGIS Pro
  2. Go to Settings, and then to Python
  3. Click Manage Environments
  4. Click on the Clone Button next to arcgispro-py3 (and do not use Clone Default)
  5. Choose a name (eg., arcgispro-py3-dev) and location (eg., C:\Users\UserX\AppData\Local\ESRI\conda\envs)
  6. After the environment was created (which takes a while), select it and click OK
  7. Go to Add Packages, search for geopandas and click Install
  8. Go to Manage Environments again and change back to arcgispro-py3
  9. Now you can use: C:\Users\UserX\AppData\Local\ESRI\conda\envs\arcgispro-py3-dev\python.exe

Script

import pandas as pd
import geopandas as gpd

from shapely.geometry import LineString

# Read CSV File
df = pd.read_csv("trips.csv")

# Create Points
points = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.latitude, df.longitude))
points = points.drop(columns=["latitude", "longitude"])

# Make sure Points are ordered (important)
points = points.sort_values(["tripId", "timestamp"])

# Create Lines
lines = points.groupby(["tripId"])["geometry"].apply(lambda x: LineString(x.tolist()))
lines = gpd.GeoDataFrame(lines, geometry="geometry", crs="EPSG:4326")

# Get Intersection Points
intersection_points = gpd.GeoDataFrame({"tripA":[], "tripB":[], "geometry":[]}, geometry="geometry")
for index in combinations(lines.index, 2):
    
    combination = lines.loc[index,:]
    
    geometries = combination["geometry"].tolist()
    point = geometries[0].intersection(geometries[1])

    if point:  # LINESTRING EMPTY evaluates to false

        trips = combination["tripId"].tolist()
        row = pd.Series([trips[0], trips[1], point], index=intersection_points.columns)
        intersection_points = intersection_points.append(row, ignore_index=True)
    
# Write Shape Files
lines.to_file("trips.shp")
intersection_points.to_file("intersections.shp")

Example CSV File

"timestamp","tripId","longitude","latitude"
"2021-07-05 10:35:04","1866491","8.167035","53.160473"
"2021-07-05 10:35:03","1866491","8.167023","53.160469"
"2021-07-05 10:35:02","1866491","8.167007","53.160459"
"2021-07-05 10:35:01","1866491","8.166987","53.160455"
"2021-07-05 10:35:00","1866491","8.166956","53.160448"
"2021-07-05 10:34:20","1866491","8.167286","53.15919"
"2021-07-05 10:34:19","1866491","8.167328","53.15918"
"2021-07-05 10:34:18","1866491","8.16735","53.159165"
"2021-07-05 10:34:17","1866491","8.167371","53.159148"
"2021-07-05 10:34:16","1866491","8.167388","53.159124"
"2021-07-05 10:34:15","1866491","8.167399","53.159105"
"2021-06-30 20:25:30","1862861","8.211288","53.150848"
"2021-06-30 20:25:29","1862861","8.211264","53.150851"
"2021-06-30 20:25:28","1862861","8.211269","53.150842"
"2021-06-30 20:25:27","1862861","8.211273","53.150836"
"2021-06-30 20:25:26","1862861","8.211279","53.150836"
"2021-06-30 20:25:25","1862861","8.211259","53.150848"
"2021-06-30 20:25:24","1862861","8.211263","53.15085"
"2021-06-30 20:25:21","1862861","8.211455","53.150782"
"2021-06-30 20:25:20","1862861","8.211453","53.150786"
"2021-06-30 20:25:19","1862861","8.211449","53.150792"
"2021-06-30 20:25:18","1862861","8.166607","53.159654"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment