Clipping the LRS About Polygon Data

Intro

I've done a lot of things close to polygon clipping about the LRS but never actually fully implemented in a generic tool for doing this. Polygon clipping refers to breaking roads into events where they exist inside of a certain polygon for example urban areas. This process to my knowledge isn't automated, so I set out to build a simple command line tool to help aid in the process.



Why It Matters

Many LRS events are simply just clipped polygon layers evented to the LRS, urban areas for example, if we can make a process somewhat automated but generic enough to be useful for all polygon clipping, we can run this process semi-regularly to ensure these polygon clipped layers are always correct. It can be performed quickly enough that it could be done in nightly processes already done by the server. For example clipping zip codes for the state of WV takes just 10 seconds to event 100k lines. For polygon dense data sets it is much slower but still manageable 70k census blocks takes 10 minutes to event currently.

Process - Tool Creation

To start we will use a roads.geobuf file for a given DOT organization as our road network. A geobuf file is just a binary geojson file that can be iteratively read/written. So when we loop through roads we only deal with one road at a time not the entire data set sitting in memory. Likewise we will take a set of polygons in a geojson or geobuf format, this file just needs to have the polygons you wan't to cut the LRS against.

Parameter - Fields Passed To Events

A comma seperated CLI input denotes which columns or properties in the polygon dataset we want to carry over into the events being produced for example: "ZIP,GEOID10,BLOCKID" would bring those fields into the event table file output.

Parameters - Visualization

The "--polygons" and "--styled" cli parameters indicate you want the polygons to be added to the output geojson or geobuf file for visualization. The styled parameter indicates you want to style each polygon by a color as well as the corresponding events.

Building the Clip

The clipping part of this algorithm is pretty simple ray-casting for point-in-polygon for every point in a line, as we walk through the line if the current point and the previous points polygon ids don't match were straddling an intersection. Get the intersected point between the polygon and the two points, then do some geometric distance -> lrs distance interpolation using the lrs measures from the two points. Looking something like:

The psuedo code below describes getting the LRS distance From an Intersection

pt1,pt2 // points in different polygons 
lrsd1,lrsd2 // distances related to pt1, pt2 on the lrs
geo_dist = geometric_dist(pt1,pt2)
lrs_dist = lrsd2-lrsd1 

// getting the intersection point using the lines we have
pt = get_intersection([pt1,pt2],polygon_lines)
dist_pt1 = geometrict_dist(pt1,pt) // gets the distance from pt1 to the intersection point 

// finally getting the lrs measure 
lrs_measure = lrsd1 + (dist_pt1/geo_dist)*lrs_dist 

The rest of the algorithm is mostly bookkeeping , when you hit an intersection add the new line on the list of event features, and carry over the current_mp and pt as the first pt in the new_line. Finally cleaning up and adding the last line after we've iterated through all the points. Making sure to pass in the fields from the polygon we were just in to the new event feature.

Usage

The final CLI command to run the polygon clip is below, as you can see you pass the road network geobuf, the polygon geojson, define the output filename, and finally pass the fields in the polygon geojson file that you wish to carry into the events.

lrspoly cut -f ~/go_files/lrs_data/roads.geobuf -o b.geojson -p zip_codes.geojson -c ZIP,PO_NAME --styled --polygons

The output file features has properties that you would find in a normal event table, RouteID, BMP, and EMP as well as the carried over fields from polygon it resides in.

Previous
Previous

HPMS Basemap Part 1: Data Analysis to Determine Shape / Distribution

Next
Next

LRS From Base Map: A Feasibility Study