Compare commits
15 Commits
etta-drone
...
master
Author | SHA1 | Date |
---|---|---|
chrisd | e9b95d3a85 | 5 years ago |
chrisd | d22acb6e09 | 5 years ago |
chrisd | 5edc0b0aa6 | 5 years ago |
chrisd | 3e484f9f2f | 5 years ago |
Dan Howe | 7f046d84a5 | 5 years ago |
Dan Howe | 00791e5317 | 5 years ago |
Dan Howe | f6b2f8f38e | 5 years ago |
Dan Howe | e334ed888e | 5 years ago |
Dan Howe | cfba2b500a | 5 years ago |
Dan Howe | 793aad14c3 | 5 years ago |
Dan Howe | 4a31ae3f34 | 5 years ago |
chrisd | e59f3aedd1 | 5 years ago |
chrisd | 3220cf60ad | 5 years ago |
chrisd | 993296cef8 | 5 years ago |
chrisd | 82f22df890 | 5 years ago |
@ -0,0 +1,92 @@
|
||||
"""polyline_to_points.py
|
||||
Extract interpolated points along transects in a shapefile.
|
||||
|
||||
D. Howe
|
||||
d.howe@wrl.unsw.edu.au
|
||||
2020-02-19
|
||||
"""
|
||||
import sys
|
||||
import argparse
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import geopandas as gpd
|
||||
from shapely.geometry import LineString
|
||||
|
||||
|
||||
def extract(shp_path, spacing=1, field=None):
|
||||
|
||||
rows = []
|
||||
shp = gpd.read_file(shp_path)
|
||||
|
||||
if field is None:
|
||||
# Assume profile name is second field in shapefile
|
||||
field = shp.columns[1]
|
||||
|
||||
for i, line in shp.iterrows():
|
||||
g = line.geometry
|
||||
chainages = np.arange(0, g.length, step=spacing)
|
||||
for chainage in chainages:
|
||||
easting, northing = g.interpolate(chainage).xy
|
||||
|
||||
row = {
|
||||
'ProfileNum': line[field],
|
||||
'Easting': easting[0],
|
||||
'Northing': northing[0],
|
||||
'Chainage': chainage,
|
||||
}
|
||||
rows.append(row)
|
||||
|
||||
# Create output dataframe
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
# Re-order columns
|
||||
df = df[['ProfileNum', 'Easting', 'Northing', 'Chainage']]
|
||||
|
||||
# Export to csv
|
||||
csv_path = shp_path.replace('.shp', '.csv')
|
||||
df.to_csv(csv_path, index=False)
|
||||
|
||||
|
||||
def main():
|
||||
example_text = """examples:
|
||||
# Extract points at default spacing (1m)
|
||||
$ python polyline_to_points.py path/to/shp
|
||||
|
||||
# Extract points at 5m increments
|
||||
$ python polyline_to_points.py -s 5 path/to/shp
|
||||
|
||||
# Use profile names from field "ProfileID" in the attribute table
|
||||
$ python polyline_to_points.py -f ProfileID path/to/shp
|
||||
"""
|
||||
# Set up command line arguments
|
||||
parser = argparse.ArgumentParser(
|
||||
epilog=example_text,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument('shp_path',
|
||||
metavar='SHAPEFILE',
|
||||
help='path to input shapefile')
|
||||
parser.add_argument('-s',
|
||||
'--spacing',
|
||||
metavar='SPACING',
|
||||
default=1,
|
||||
type=int,
|
||||
help='space between points (default=1)')
|
||||
parser.add_argument('-f',
|
||||
'--field',
|
||||
metavar='FIELDNAME',
|
||||
help='profile field name in attribute table')
|
||||
|
||||
# Print usage if no arguments are provided
|
||||
if len(sys.argv) == 1:
|
||||
parser.print_help(sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Parse arguments
|
||||
args = parser.parse_args()
|
||||
|
||||
# Combine images
|
||||
extract(**vars(args))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue