Skip to content

Commit

Permalink
Merge pull request #12 from MIERUNE/feature/gridstats-gassan
Browse files Browse the repository at this point in the history
メッシュ統計: 秘匿と合算を正しく扱う
  • Loading branch information
ciscorn authored Sep 19, 2023
2 parents 878e2cb + bd31fd2 commit d7db2a5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 39 deletions.
71 changes: 55 additions & 16 deletions japanese_grids/algorithms/load_estat_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
QgsFeatureSink,
QgsField,
QgsFields,
QgsGeometry,
QgsLineString,
QgsPolygon,
QgsProcessing,
Expand Down Expand Up @@ -134,7 +135,18 @@ def shortHelpString(self) -> str:
def _open_file(self, filename: str) -> TextIO:
return open(filename, encoding="cp932") # noqa: SIM115

def processAlgorithm(
def _bbox_to_polygon(self, bbox: tuple[float, float, float, float]) -> QgsPolygon:
lng0, lat0, lng1, lat1 = bbox
exterior = (
(lng0, lat0),
(lng0, lat1),
(lng1, lat1),
(lng1, lat0),
(lng0, lat0),
)
return QgsPolygon(QgsLineString(exterior))

def processAlgorithm( # noqa: C901
self,
parameters: dict[str, Any],
context: QgsProcessingContext,
Expand Down Expand Up @@ -181,7 +193,7 @@ def processAlgorithm(
self.OUTPUT,
context,
fields,
QgsWkbTypes.Polygon,
QgsWkbTypes.MultiPolygon,
crs_to_assign,
)
result[self.OUTPUT] = dest_id
Expand All @@ -192,30 +204,57 @@ def processAlgorithm(
for idx in range(len(columns))
if columns[idx] in ("KEY_CODE", "HTKSAKI", "GASSAN")
]

num_columns = len(columns)
key_code_idx = columns.index("KEY_CODE")
gassan_idx = columns.index("GASSAN")
htksyori_idx = columns.index("HTKSYORI")

for row in _iter_rows(f):
feat = QgsFeature()
feat.setFields(fields, initAttributes=True)
assert len(row) == num_columns
geoms = []
skip = False

for idx, s in enumerate(row):
# 秘匿処理されているパッチはスキップ
if idx == htksyori_idx and s == "2":
skip = True
break

if s is None or s == "*":
continue
if idx in str_fields:
feat.setAttribute(idx, s)
else:
feat.setAttribute(idx, int(s))
if idx == key_code_idx and (bbox := grid_square_code_to_bbox(s)):
lng0, lat0, lng1, lat1 = bbox
exterior = (
(lng0, lat0),
(lng0, lat1),
(lng1, lat1),
(lng1, lat0),
(lng0, lat0),

feat.setAttribute(
idx,
s if idx in str_fields else int(s),
)

# Geometry
if idx == key_code_idx:
if bbox := grid_square_code_to_bbox(s):
geoms.append(self._bbox_to_polygon(bbox))
elif idx == gassan_idx:
bboxes = [grid_square_code_to_bbox(s) for s in s.split(";")]
geoms.extend(
self._bbox_to_polygon(bbox)
for bbox in bboxes
if bbox is not None
)
geom = QgsPolygon(QgsLineString(exterior))
feat.setGeometry(geom)

# 秘匿処理されているパッチはスキップ
if skip:
continue

# パッチのジオメトリを作成
if len(geoms) == 1:
feat.setGeometry(geoms[0])
elif len(geoms) > 1:
# 合算されている場合はジオメトリを合成
feat.setGeometry(
QgsGeometry.unaryUnion(QgsGeometry(p) for p in geoms)
)

sink.addFeature(feat, QgsFeatureSink.FastInsert)

Expand Down
45 changes: 23 additions & 22 deletions japanese_grids/algorithms/utils/gridsquare_to_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,35 @@ def grid_square_code_to_bbox( # noqa: C901
lat = int(code[:2])
lng = int(code[2:4]) + 100
if length == 4:
return (lng / 1.5, lat, lng + 1, (lat + 1) / 1.5)
return (lng, lat / 1.5, lng + 1, (lat + 1) / 1.5)

lat += int(code[4]) * 0.125
lng += int(code[5]) * 0.125
if length == 6:
return (lng / 1.5, lat, lng + 0.125, (lat + 0.125) / 1.5)
return (lng, lat / 1.5, lng + 0.125, (lat + 0.125) / 1.5)

lat10 = int(code[6]) * 0.125
lng10 = int(code[7]) * 0.125
if length == 8:
lat = (lat + lat10 / 10) / 1.5
lng = lng + lng10 / 10
return (lng, lat, lng + 0.0125, lat + 0.0125 / 1.5)
lat1 = lat + lat10 / 10
lng1 = lng + lng10 / 10
lat2 = lat + (lat10 + 0.125) / 10
lng2 = lng + (lng10 + 0.125) / 10
return (lng1, lat1 / 1.5, lng, lat / 1.5)

a = code[8]
if a not in _VALID_QUAD:
return None
if a == "3" or a == "4":
lat10 += 0.0625
if a == "1" or a == "4":
if a == "2" or a == "4":
lng10 += 0.0625
if length == 9:
lat = (lat + lat10 / 10) / 1.5
lng = lng + lng10 / 10
return (lng, lat, lng + 0.00625, lat + 0.00625 / 1.5)
lat1 = lat + lat10 / 10
lng1 = lng + lng10 / 10
lat2 = lat + (lat10 + 0.0625) / 10
lng2 = lng + (lng10 + 0.0625) / 10
return (lng1, lat1 / 1.5, lng2, lat2 / 1.5)

a = code[9]
if a not in _VALID_QUAD:
Expand All @@ -51,9 +55,11 @@ def grid_square_code_to_bbox( # noqa: C901
if a == "2" or a == "4":
lng10 += 0.03125
if length == 10:
lat = (lat + lat10 / 10) / 1.5
lng = lng + lng10 / 10
return (lng, lat, lng + 0.003125, lat + 0.003125 / 1.5)
lat1 = lat + lat10 / 10
lng1 = lng + lng10 / 10
lat2 = lat + (lat10 + 0.03125) / 10
lng2 = lng + (lng10 + 0.03125) / 10
return (lng1, lat1 / 1.5, lng2, lat2 / 1.5)

a = code[10]
if a not in _VALID_QUAD:
Expand All @@ -62,13 +68,8 @@ def grid_square_code_to_bbox( # noqa: C901
lat10 += 0.015625
if a == "2" or a == "4":
lng10 += 0.015625
lat = (lat + lat10 / 10) / 1.5
lng = lng + lng10 / 10
return (lng, lat, lng + 0.0015625, lat + 0.0015625 / 1.5)


if __name__ == "__main__":
latlng = grid_square_code_to_bbox("M55385272342")
print(latlng)
latlng = grid_square_code_to_bbox("M55385272342")
print(latlng)
lat1 = lat + lat10 / 10
lng1 = lng + lng10 / 10
lat2 = lat + (lat10 + 0.015625) / 10
lng2 = lng + (lng10 + 0.015625) / 10
return (lng1, lat1 / 1.5, lng2, lat2 / 1.5)
2 changes: 1 addition & 1 deletion japanese_grids/metadata.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[general]
name=Japanese Grid Mesh
version=0.2.0
version=0.2.1
qgisMinimumVersion=3.6
description=Create common grid squares used in Japan. 日本で使われている「標準地域メッシュ」および「国土基本図図郭」を作成できるほか、国勢調査や経済センサスなどの「地域メッシュ統計」のCSVファイルを読み込むこともできます。プロセッシングツールボックスから利用できます。
author=MIERUNE Inc.
Expand Down

0 comments on commit d7db2a5

Please sign in to comment.