|
- import subprocess
- import sys
- import json
-
- def get_centroids(file, field_name):
- results = {}
- for entry in subprocess.check_output(
- [
- "mapshaper",
- "-i",
- file,
- "-each",
- "\"console.log(JSON.stringify({{name: {0}, lon: this.centroidX, lat: this.centroidY}}))\"".format(field_name)
- ],
- shell = True,
- stderr = subprocess.STDOUT
- ).decode("utf-8").split("\n"):
- print(entry)
- parts = json.loads(entry)
- results[parts["name"]] = {
- "lon": float(parts["lon"]),
- "lat": float(parts["lat"])
- }
-
- return results
-
- def extract_shape(file, field_name, field_value):
- lat, lon = subprocess.check_output(
- [
- "mapshaper",
- "-i",
- file,
- "-filter",
- "\"{0} == {1}\"".format(field_name, field_value),
- "-each",
- "\"console.log(this.centroidX, this.centroidY)"
- ]
- )
-
- if __name__ == "__main__":
- print(get_centroids(sys.argv[1], sys.argv[2]))
|