浏览代码

Add some changes to the scripts

master
Fen Dweller 4 年前
父节点
当前提交
e65667b1b0
共有 2 个文件被更改,包括 109 次插入2 次删除
  1. +99
    -2
      scripts/mapshaper/map-coords.py
  2. +10
    -0
      scripts/mapshaper/states.json

+ 99
- 2
scripts/mapshaper/map-coords.py 查看文件

@@ -1,6 +1,7 @@
import json
import sys
import subprocess
from tqdm import tqdm

config = json.load(open(sys.argv[1]))

@@ -61,6 +62,7 @@ elif config["mode"] == "filter":
shell=True,
stderr=subprocess.STDOUT
)

for item in config["items"]:
CMD = (
@@ -91,14 +93,15 @@ elif config["mode"] == "filter":
if not found:
print("Did not find ", item["name"])
continue

CMD = (
f"""mapshaper -i temp.shp """
f"""-filter "{config["base-filter"]}" """
f"""-filter "{item["filter"]}" """
f"""-simplify interval={config["simplify-size"]} """
f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """
f"""-info """ +
f"""-info """
f"""-style stroke-width={config["stroke-width"]} """
f"""-o "../../{config["directory"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """
)
@@ -123,3 +126,97 @@ elif config["mode"] == "filter":
results.append([item["name"], height])
print(results)
elif config["mode"] == "forms":
results = []
CMD = (
f"""mapshaper -i {config["shapefile"]} """
f"""-filter "{config["base-filter"]}" """
f"""-o temp.shp"""
)

result = subprocess.check_output(
CMD,
shell=True,
stderr=subprocess.STDOUT
)

CMD = (
f"""mapshaper -i temp.shp """
f"""encoding=utf-8 """
f"""-each "console.log(JSON.stringify({{id: this.id, name: name, key: {config["form-key"]} }}))" """
f"""-split """
f"""-o tmp/ format=topojson singles target=* """
)

try:
result = subprocess.check_output(
CMD,
shell=True
)
except subprocess.CalledProcessError as e:
print(e.output)

entities = []

for line in result.decode("utf-8").split("\n"):
if len(line) > 0:
print(line)
entities.append(json.loads(line))

for item in tqdm(entities):
CMD = (
f"""mapshaper -i tmp/temp-{item["id"] + 1}.json """
f"""encoding=utf-8 """
f"""-simplify interval=1000 """
f"""-info """
)

result = subprocess.check_output(
CMD,
shell=True,
stderr=subprocess.STDOUT
)
found = False
for line in result.decode("utf-8").split("\n"):
if "Bounds:" in line:
coords = list(map(float, line[7:].strip().split(",")))
lat = coords[1] + coords[3]
lat /= 2
lon = coords[0] + coords[2]
lon /= 2
found = True
break

if not found:
print("Did not find ", item["name"])
continue

CMD = (
f"""mapshaper -i tmp/temp-{item["id"] + 1}.json """
f"""encoding=utf-8 """
f"""-simplify interval={config["simplify-size"]} """
f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """
f"""-info """
f"""-style stroke-width={config["stroke-width"]} """
f"""-o "../../{config["directory"]}/{item["key"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """
)

try:
result = subprocess.check_output(
CMD,
shell=True,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as exc:
print(exc.output.decode("utf-8"))
continue

for line in result.decode("utf-8").split("\n"):
if "Bounds:" in line:
coords = list(map(float, line[7:].strip().split(",")))
height = coords[3] - coords[1]
height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin
results.append({"name": item["name"], "form": item["key"], "height": round(height)})
print(results)

+ 10
- 0
scripts/mapshaper/states.json 查看文件

@@ -0,0 +1,10 @@
{
"name": "Straits",
"directory": "/media/naturals/States/",
"shapefile": "E:/macrovision/mapshaper/states/ne_10m_admin_1_states_provinces_lakes.shp",
"mode": "forms",
"base-filter": "true",
"form-key": "admin",
"stroke-width": 1,
"simplify-size": 100
}

正在加载...
取消
保存