Skip to content

Instantly share code, notes, and snippets.

@vincentsarago
Created September 6, 2019 03:13
Show Gist options
  • Save vincentsarago/09f84c1e9f4e2154a919692764595306 to your computer and use it in GitHub Desktop.
Save vincentsarago/09f84c1e9f4e2154a919692764595306 to your computer and use it in GitHub Desktop.
import json
import click
import cligj
from shapely.geometry import shape, mapping, MultiPolygon
from shapely.affinity import translate
@click.command()
@click.pass_context
@cligj.features_in_arg
@cligj.sequence_opt
def centroid(ctx, features, sequence):
"""Get Centroid."""
for f in features:
geom = shape(f['geometry'])
if f["properties"]["RINGS_OK"] == 2:
geom = MultiPolygon(
[
translate(g, xoff=360) if g.bounds[0] < 0 else g for g in geom
]
)
centroid = geom.centroid
if centroid.x > 180:
centroid = translate(centroid, xoff=-360)
f['geometry'] = mapping(centroid)
print(json.dumps(f))
# centroid = geom.centroid
# f['geometry'] = mapping(centroid)
if __name__ == '__main__':
centroid()
@vincentsarago
Copy link
Author

fio cat WRS2_descending.shp | python centroid_landsat.py  | jq -c 'del(.properties.PERIMETER) | del(.properties.AREA) | del(.properties.RINGS_NOK) | del(.properties.RINGS_OK)' | fio collect > WRS2_descending_centroid.geojson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment