Skip to content

Commit

Permalink
Remove calcuration of representative points
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscorn committed May 19, 2023
1 parent 3abcece commit 8d73647
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 31 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ mojxml2ogr = 'mojxml.__main__:main'
python = ">=3.8.7,<4.0"
lxml = "^4.9.2"
pyproj = "^3.4.1"
shapely = "^2.0.1"
fiona = "^1.9.1"
click = "^8.1.3"

Expand Down
21 changes: 21 additions & 0 deletions src/mojxml/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 MIERUNE Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion src/mojxml/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Command line interface for mojxml."""
"""Command line interface for mojxml"""

import logging
from pathlib import Path
Expand Down
2 changes: 1 addition & 1 deletion src/mojxml/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Constant values."""
"""Constant values"""

from typing import Mapping, Optional

Expand Down
14 changes: 3 additions & 11 deletions src/mojxml/parse.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Parse MOJ MAP XML files."""
"""Parse MOJ MAP XML files"""

from dataclasses import dataclass
from typing import Dict, List, Tuple, TypedDict

import lxml.etree as et
import pyproj
from shapely.geometry import MultiPolygon

from .constants import CRS_MAP
from .constants import XML_NAMESPACES as _NS
Expand All @@ -17,14 +16,14 @@

@dataclass
class ParseOptions:
"""Options for parsing XMLs."""
"""Options for parsing XMLs"""

include_arbitrary_crs: bool = False
include_chikugai: bool = False


class Feature(TypedDict):
"""GeoJSON-like feature representation."""
"""GeoJSON-like feature representation"""

type: str
geometry: Dict[str, list]
Expand Down Expand Up @@ -160,20 +159,13 @@ def _parse_features(
"市区町村名": None,
"座標系": None,
"測地系判別": None,
"代表点経度": None,
"代表点緯度": None,
}
geometry = None
for entry in fude:
key = entry.tag.split("}")[1]
if key == "形状":
coordinates = surfaces[entry.attrib["idref"]]
geometry = {"type": "MultiPolygon", "coordinates": coordinates}
rep_point = MultiPolygon(
(p[0], p[1:]) for p in coordinates
).representative_point()
properties["代表点経度"] = rep_point.x
properties["代表点緯度"] = rep_point.y
else:
value = entry.text
properties[key] = value
Expand Down
20 changes: 10 additions & 10 deletions src/mojxml/process/executor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Run conversion in parallel."""
"""Run conversion process in parallel."""

import concurrent.futures
import os
Expand All @@ -9,22 +9,22 @@


class BaseExecutor(metaclass=ABCMeta):
"""Executor for processing files."""
"""Executor for processing files"""

def __init__(self, options: ParseOptions) -> None:
"""Initialize."""
"""Initialize"""
self.options = options

@abstractmethod
def iter_process(
self,
src_iter: Iterable[bytes],
) -> Iterable[List[Feature]]:
"""Convert XMLs to OGR features."""
"""Convert XMLs to OGR features"""


class WorkerPoolExecutor(BaseExecutor, metaclass=ABCMeta):
"""Executor implemeted with worker pool."""
"""Executor implemeted with worker pool"""

@abstractmethod
def _get_executor(self, max_workers: int) -> concurrent.futures.Executor:
Expand All @@ -34,7 +34,7 @@ def iter_process(
self,
src_iter: Iterable[bytes],
) -> Iterable[List[Feature]]:
"""Convert XMLs to OGR features."""
"""Convert XMLs to OGR features"""
max_workers = os.cpu_count() or 1
with self._get_executor(max_workers=max_workers) as executor:
futs = []
Expand All @@ -58,26 +58,26 @@ def iter_process(


class ProcessPoolExecutor(WorkerPoolExecutor):
"""Process paralelly with ProcessPoolExecutor."""
"""Process in parallel with ProcessPoolExecutor"""

def _get_executor(self, max_workers: int) -> concurrent.futures.Executor:
max_workers = os.cpu_count() or 1
return concurrent.futures.ProcessPoolExecutor(max_workers=max_workers)


class ThreadPoolExecutor(WorkerPoolExecutor):
"""Process paralelly with ThreadPoolExecutor."""
"""Process in parallel with ThreadPoolExecutor"""

def _get_executor(self, max_workers: int) -> concurrent.futures.Executor:
max_workers = (os.cpu_count() or 1) * 2
return concurrent.futures.ThreadPoolExecutor(max_workers=max_workers)


class SingleThreadExecutor(BaseExecutor):
"""Process files with single-thread (normal) iterator."""
"""Process files with single-thread (normal) iterator"""

def iter_process(self, src_iter: Iterable[bytes]) -> Iterable[List[Feature]]:
"""Convert XMLs to OGR features."""
"""Convert XMLs to OGR features"""
for src in src_iter:
yield parse_raw(src, options=self.options)

Expand Down
8 changes: 4 additions & 4 deletions src/mojxml/reader.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Handle XMLs and single/doule-zipped archives of MOJXML trnsparently."""
"""Handle XML and ZIP sources trnsparently"""

from pathlib import Path
from typing import Iterable, List
from zipfile import ZipFile


def iter_content_xmls(src_paths: List[Path]) -> Iterable[bytes]:
"""Iterate XML contents from given zips and xmls."""
"""Iterate XML contents from given zips and xmls"""
for src_path in src_paths:
src_path = Path(src_path)
if src_path.suffix == ".xml":
Expand All @@ -20,10 +20,10 @@ def iter_content_xmls(src_paths: List[Path]) -> Iterable[bytes]:


class MojXMLZipFile(ZipFile):
"""Handles single/doule-zipped archives of MOJ XMLs transparently."""
"""法務省登記所備付地図データの多段zip圧縮されたアーカイブを扱う"""

def iter_xml_contents(self) -> Iterable[bytes]:
"""Iterate XML contents from given zips."""
"""Iterate XML contents from given zips"""
for name in self.namelist():
if name.endswith(".zip"):
yield self._extract_xml_content(name[:-4])
Expand Down
4 changes: 1 addition & 3 deletions src/mojxml/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Schema for OGR output."""
"""Schema for OGR output"""

import typing
from collections import OrderedDict
Expand Down Expand Up @@ -28,8 +28,6 @@ class _SchemaType(TypedDict):
("小字名", "str"),
("地番", "str"),
("筆界未定構成筆", "str"),
("代表点経度", "float"),
("代表点緯度", "float"),
("精度区分", "str"),
("座標値種別", "str"),
]
Expand Down

0 comments on commit 8d73647

Please sign in to comment.