-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LabelCenterline function #426
Open
quincylvania
wants to merge
14
commits into
openmaptiles:master
Choose a base branch
from
quincylvania:sql-label-centerline
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
72f5f15
Add LabelCenterline function
quincylvania c17a3d7
Polygon is implied at this step
quincylvania 3d5b0c8
Convert function from string definitions to plpgsql begin/end
quincylvania 966c0a5
Use more descriptive variable names
quincylvania 3d77ebc
Multipoint-based hull causes some issues
quincylvania 5fe3a05
2x faster execution by avoiding ST_Difference entirely
quincylvania 8eba5d5
Create separate centerline for each part of a multipolygon instead of…
quincylvania 9bf39d7
Uppercase `as`
quincylvania 4d3474c
Avoid unnecessary plpgsql. Make functions immutable, strict, parallel…
quincylvania ada7653
Name remaining CTE tables
quincylvania 5c47d32
Add parameters to control centerline creation
quincylvania a83ebde
Don't need drop
quincylvania d1586bd
Add parameter to enable multiple branches in the output (useful for p…
quincylvania 9f75d70
Support holes
quincylvania File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/****************************************************************************** | ||
### LabelCenterline ### | ||
|
||
Given a polygon or multipolygon, calculates a linestring suitable for a label. | ||
|
||
__Parameters:__ | ||
|
||
- `geometry` inGeometry - A polygon or multipolygon. | ||
|
||
__Returns:__ `geometry(multiline)` | ||
******************************************************************************/ | ||
CREATE OR REPLACE FUNCTION CountDisconnectedEndpoints(polyline geometry, testline geometry) | ||
RETURNS integer AS $$ | ||
BEGIN | ||
RETURN ST_NPoints(ST_RemoveRepeatedPoints(ST_Points(polyline))) | ||
- ST_NPoints(ST_RemoveRepeatedPoints(ST_Points(ST_Difference(polyline, testline)))) | ||
- ST_NPoints(testline) + 2; | ||
END | ||
$$ LANGUAGE plpgsql; | ||
CREATE OR REPLACE FUNCTION TrimmedCenterline(inPolyline geometry) | ||
RETURNS geometry AS $$ | ||
DECLARE outPolyline geometry; | ||
BEGIN | ||
WITH tbla AS ( | ||
quincylvania marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SELECT inPolyline as polyline, (ST_Dump(inPolyline)).geom as edge | ||
), | ||
tblb AS ( | ||
SELECT polyline, edge as shortestBranchLine | ||
FROM tbla | ||
WHERE CountDisconnectedEndpoints(polyline, edge) > 0 | ||
ORDER BY ST_Length(edge) ASC | ||
LIMIT 1 | ||
), | ||
tblc AS ( | ||
SELECT ST_LineMerge(ST_Difference(polyline, shortestBranchLine)) as polyline | ||
FROM tblb | ||
) | ||
SELECT TrimmedCenterline(polyline) as polyline | ||
FROM tblc | ||
WHERE ST_NumGeometries(polyline) > 1 | ||
UNION ALL | ||
SELECT polyline INTO outPolyline FROM tblc; | ||
RETURN outPolyline; | ||
END | ||
$$ LANGUAGE plpgsql; | ||
CREATE OR REPLACE FUNCTION LabelCenterline(inGeometry geometry) | ||
RETURNS geometry AS $$ | ||
DECLARE outPolyline geometry; | ||
BEGIN | ||
WITH tbla AS ( | ||
quincylvania marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SELECT inGeometry as inPolygon WHERE ST_GeometryType(inGeometry) = 'ST_Polygon' | ||
UNION ALL | ||
SELECT ST_ConcaveHull(ST_Simplify(inGeometry, 25), 0.2) as inPolygon WHERE ST_GeometryType(inGeometry)='ST_MultiPolygon' | ||
quincylvania marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
tblb AS ( | ||
SELECT ST_MakePolygon(ST_ExteriorRing(inPolygon)) as shellPolygon | ||
FROM tbla | ||
), | ||
tblc AS ( | ||
SELECT shellPolygon, (ST_Dump(ST_VoronoiLines(ST_LineInterpolatePoints(ST_Boundary(shellPolygon), 0.0075)))).geom as voroniLines | ||
FROM tblb | ||
), | ||
tbld AS ( | ||
SELECT ST_LineMerge(ST_Collect(voroniLines)) as voroniPolyline | ||
FROM tblc | ||
WHERE ST_Contains(shellPolygon, voroniLines) | ||
) | ||
SELECT ST_ChaikinSmoothing(ST_SimplifyPreserveTopology(TrimmedCenterline(voroniPolyline), 80), 3, false) INTO outPolyline FROM tbld; | ||
RETURN outPolyline; | ||
END | ||
$$ LANGUAGE plpgsql; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An explanation of the equation would be good here.