less copy protection, more size visualization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

41 line
1.1 KiB

  1. import subprocess
  2. import sys
  3. import json
  4. def get_centroids(file, field_name):
  5. results = {}
  6. for entry in subprocess.check_output(
  7. [
  8. "mapshaper",
  9. "-i",
  10. file,
  11. "-each",
  12. "\"console.log(JSON.stringify({{name: {0}, lon: this.centroidX, lat: this.centroidY}}))\"".format(field_name)
  13. ],
  14. shell = True,
  15. stderr = subprocess.STDOUT
  16. ).decode("utf-8").split("\n"):
  17. print(entry)
  18. parts = json.loads(entry)
  19. results[parts["name"]] = {
  20. "lon": float(parts["lon"]),
  21. "lat": float(parts["lat"])
  22. }
  23. return results
  24. def extract_shape(file, field_name, field_value):
  25. lat, lon = subprocess.check_output(
  26. [
  27. "mapshaper",
  28. "-i",
  29. file,
  30. "-filter",
  31. "\"{0} == {1}\"".format(field_name, field_value),
  32. "-each",
  33. "\"console.log(this.centroidX, this.centroidY)"
  34. ]
  35. )
  36. if __name__ == "__main__":
  37. print(get_centroids(sys.argv[1], sys.argv[2]))