tr-donati 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tr_donati/__init__.py +97 -0
- tr_donati/__main__.py +7 -0
- tr_donati/_version.py +1 -0
- tr_donati/access.py +247 -0
- tr_donati/cli.py +342 -0
- tr_donati/compliance.py +427 -0
- tr_donati/data/default_mapping.json +69 -0
- tr_donati/data/ek2_2017.json +172 -0
- tr_donati/data/service_radii.json +18 -0
- tr_donati/geometry.py +195 -0
- tr_donati/landuse.py +354 -0
- tr_donati/py.typed +0 -0
- tr_donati/report.py +343 -0
- tr_donati/requirements.py +105 -0
- tr_donati/standards.py +287 -0
- tr_donati-0.1.0.dist-info/METADATA +262 -0
- tr_donati-0.1.0.dist-info/RECORD +21 -0
- tr_donati-0.1.0.dist-info/WHEEL +5 -0
- tr_donati-0.1.0.dist-info/entry_points.txt +2 -0
- tr_donati-0.1.0.dist-info/licenses/LICENSE +21 -0
- tr_donati-0.1.0.dist-info/top_level.txt +1 -0
tr_donati/__init__.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""tr_donati: Türk imar planları için donatı alanı (sosyal ve teknik altyapı) standartları.
|
|
2
|
+
|
|
3
|
+
Social and technical infrastructure (donatı) standards for Turkish spatial plans, based
|
|
4
|
+
on EK-2 of the Mekânsal Planlar Yapım Yönetmeliği (RG 14/6/2014-29030, EK-2 as amended
|
|
5
|
+
by RG 17/5/2017-30069).
|
|
6
|
+
|
|
7
|
+
Hızlı başlangıç::
|
|
8
|
+
|
|
9
|
+
import tr_donati as td
|
|
10
|
+
|
|
11
|
+
for r in td.required_areas(120_000):
|
|
12
|
+
print(r.label, r.per_capita, r.required_area)
|
|
13
|
+
|
|
14
|
+
rep = td.check_compliance({"park": 900_000, "ilkokul": 180_000}, population=120_000)
|
|
15
|
+
print(rep.to_markdown())
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from ._version import __version__
|
|
19
|
+
from .access import (
|
|
20
|
+
DEFAULT_SERVICE_RADII,
|
|
21
|
+
Assignment,
|
|
22
|
+
CoverageResult,
|
|
23
|
+
Point,
|
|
24
|
+
default_service_radius,
|
|
25
|
+
read_distance_matrix,
|
|
26
|
+
read_points,
|
|
27
|
+
service_coverage,
|
|
28
|
+
)
|
|
29
|
+
from .compliance import (
|
|
30
|
+
CategoryResult,
|
|
31
|
+
ComplianceReport,
|
|
32
|
+
ParcelCheck,
|
|
33
|
+
ParcelInput,
|
|
34
|
+
RuleCheck,
|
|
35
|
+
check_compliance,
|
|
36
|
+
check_parcel,
|
|
37
|
+
)
|
|
38
|
+
from .geometry import haversine, polygon_area
|
|
39
|
+
from .landuse import (
|
|
40
|
+
LandUseMapping,
|
|
41
|
+
PlanFeature,
|
|
42
|
+
PlanSummary,
|
|
43
|
+
normalize_key,
|
|
44
|
+
read_csv,
|
|
45
|
+
read_geojson,
|
|
46
|
+
read_plan,
|
|
47
|
+
summarize,
|
|
48
|
+
)
|
|
49
|
+
from .report import fmt_num, standards_markdown
|
|
50
|
+
from .requirements import Requirement, population_group, required_areas, total_per_capita
|
|
51
|
+
from .standards import (
|
|
52
|
+
Category,
|
|
53
|
+
Facility,
|
|
54
|
+
PopulationGroup,
|
|
55
|
+
StandardsError,
|
|
56
|
+
StandardsTable,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
__all__ = [
|
|
60
|
+
"DEFAULT_SERVICE_RADII",
|
|
61
|
+
"Assignment",
|
|
62
|
+
"Category",
|
|
63
|
+
"CategoryResult",
|
|
64
|
+
"ComplianceReport",
|
|
65
|
+
"CoverageResult",
|
|
66
|
+
"Facility",
|
|
67
|
+
"LandUseMapping",
|
|
68
|
+
"ParcelCheck",
|
|
69
|
+
"ParcelInput",
|
|
70
|
+
"PlanFeature",
|
|
71
|
+
"PlanSummary",
|
|
72
|
+
"Point",
|
|
73
|
+
"PopulationGroup",
|
|
74
|
+
"Requirement",
|
|
75
|
+
"RuleCheck",
|
|
76
|
+
"StandardsError",
|
|
77
|
+
"StandardsTable",
|
|
78
|
+
"__version__",
|
|
79
|
+
"check_compliance",
|
|
80
|
+
"check_parcel",
|
|
81
|
+
"default_service_radius",
|
|
82
|
+
"fmt_num",
|
|
83
|
+
"haversine",
|
|
84
|
+
"normalize_key",
|
|
85
|
+
"polygon_area",
|
|
86
|
+
"population_group",
|
|
87
|
+
"read_csv",
|
|
88
|
+
"read_distance_matrix",
|
|
89
|
+
"read_geojson",
|
|
90
|
+
"read_plan",
|
|
91
|
+
"read_points",
|
|
92
|
+
"required_areas",
|
|
93
|
+
"service_coverage",
|
|
94
|
+
"standards_markdown",
|
|
95
|
+
"summarize",
|
|
96
|
+
"total_per_capita",
|
|
97
|
+
]
|
tr_donati/__main__.py
ADDED
tr_donati/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
tr_donati/access.py
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""Walking-distance / service-radius (etki alanı) checks for donatı points.
|
|
2
|
+
|
|
3
|
+
For every demand point (e.g. a housing block centroid with its population) the nearest
|
|
4
|
+
facility is found and the point counts as served if that distance is within the service
|
|
5
|
+
radius. Distances are plain Euclidean (projected CRS, metres), haversine great-circle
|
|
6
|
+
(lon/lat) or taken from a user-supplied matrix, e.g. network walking distances computed
|
|
7
|
+
elsewhere.
|
|
8
|
+
|
|
9
|
+
The Mekânsal Planlar Yapım Yönetmeliği does **not** set service radii. The defaults in
|
|
10
|
+
:data:`DEFAULT_SERVICE_RADII` are indicative walking distances commonly used in Turkish
|
|
11
|
+
planning teaching and practice; they are not verified against a primary source. Supply
|
|
12
|
+
your own radius for any formal assessment.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import csv
|
|
18
|
+
import io
|
|
19
|
+
import json
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from importlib import resources
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
|
|
24
|
+
|
|
25
|
+
from .geometry import centroid, euclidean, haversine, looks_geographic
|
|
26
|
+
|
|
27
|
+
PathLike = Union[str, Path]
|
|
28
|
+
DistanceMatrix = Mapping[Tuple[str, str], float]
|
|
29
|
+
|
|
30
|
+
METRICS = ("euclidean", "haversine")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _load_radii() -> Dict[str, float]:
|
|
34
|
+
text = (
|
|
35
|
+
resources.files("tr_donati")
|
|
36
|
+
.joinpath("data")
|
|
37
|
+
.joinpath("service_radii.json")
|
|
38
|
+
.read_text("utf-8")
|
|
39
|
+
)
|
|
40
|
+
return {k: float(v) for k, v in json.loads(text)["radii_m"].items()}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
DEFAULT_SERVICE_RADII: Dict[str, float] = _load_radii()
|
|
44
|
+
"""Indicative service radii in metres by facility id. UNVERIFIED guide values, not from
|
|
45
|
+
the regulation; override for real assessments."""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True)
|
|
49
|
+
class Point:
|
|
50
|
+
"""A demand or facility point."""
|
|
51
|
+
|
|
52
|
+
id: str
|
|
53
|
+
x: float
|
|
54
|
+
y: float
|
|
55
|
+
weight: float = 1.0
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class Assignment:
|
|
60
|
+
"""Nearest facility of one demand point."""
|
|
61
|
+
|
|
62
|
+
demand_id: str
|
|
63
|
+
facility_id: Optional[str]
|
|
64
|
+
distance: Optional[float]
|
|
65
|
+
covered: bool
|
|
66
|
+
weight: float
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass
|
|
70
|
+
class CoverageResult:
|
|
71
|
+
"""Outcome of :func:`service_coverage`."""
|
|
72
|
+
|
|
73
|
+
radius: float
|
|
74
|
+
metric: str
|
|
75
|
+
assignments: List[Assignment] = field(default_factory=list)
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def total_weight(self) -> float:
|
|
79
|
+
"""Sum of demand weights (e.g. population)."""
|
|
80
|
+
return sum(a.weight for a in self.assignments)
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def covered_weight(self) -> float:
|
|
84
|
+
"""Weight of demand points within the radius."""
|
|
85
|
+
return sum(a.weight for a in self.assignments if a.covered)
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def coverage_ratio(self) -> Optional[float]:
|
|
89
|
+
"""covered_weight / total_weight."""
|
|
90
|
+
tw = self.total_weight
|
|
91
|
+
return self.covered_weight / tw if tw > 0 else None
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def uncovered(self) -> List[Assignment]:
|
|
95
|
+
"""Demand points outside the radius (or with no reachable facility)."""
|
|
96
|
+
return [a for a in self.assignments if not a.covered]
|
|
97
|
+
|
|
98
|
+
def mean_distance(self) -> Optional[float]:
|
|
99
|
+
"""Weight-averaged distance to the nearest facility (reachable points only)."""
|
|
100
|
+
pairs = [(a.distance, a.weight) for a in self.assignments if a.distance is not None]
|
|
101
|
+
w = sum(p[1] for p in pairs)
|
|
102
|
+
if w <= 0:
|
|
103
|
+
return None
|
|
104
|
+
return sum(d * wt for d, wt in pairs if d is not None) / w
|
|
105
|
+
|
|
106
|
+
def to_markdown(self, title: Optional[str] = None) -> str:
|
|
107
|
+
"""Turkish Markdown report."""
|
|
108
|
+
from .report import coverage_markdown
|
|
109
|
+
|
|
110
|
+
return coverage_markdown(self, title=title)
|
|
111
|
+
|
|
112
|
+
def to_csv(self) -> str:
|
|
113
|
+
"""Per-demand-point CSV."""
|
|
114
|
+
from .report import coverage_csv
|
|
115
|
+
|
|
116
|
+
return coverage_csv(self)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def default_service_radius(facility: str) -> Optional[float]:
|
|
120
|
+
"""Indicative (unverified) service radius in metres, or ``None``."""
|
|
121
|
+
return DEFAULT_SERVICE_RADII.get(facility)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def service_coverage(
|
|
125
|
+
demand: Sequence[Point],
|
|
126
|
+
facilities: Sequence[Point],
|
|
127
|
+
radius: float,
|
|
128
|
+
metric: str = "euclidean",
|
|
129
|
+
distances: Optional[DistanceMatrix] = None,
|
|
130
|
+
) -> CoverageResult:
|
|
131
|
+
"""Nearest-facility distance and coverage for demand points.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
demand: demand points; ``weight`` is typically population.
|
|
135
|
+
facilities: facility points.
|
|
136
|
+
radius: service radius (same unit as the distances, normally metres).
|
|
137
|
+
metric: ``"euclidean"`` (projected coordinates) or ``"haversine"`` (lon/lat).
|
|
138
|
+
distances: optional ``{(demand_id, facility_id): distance}`` matrix (e.g.
|
|
139
|
+
network distances). When given, only listed pairs are considered reachable.
|
|
140
|
+
|
|
141
|
+
Example:
|
|
142
|
+
>>> d = [Point("a", 0, 0, 100), Point("b", 1000, 0, 50)]
|
|
143
|
+
>>> f = [Point("okul", 300, 400)]
|
|
144
|
+
>>> r = service_coverage(d, f, radius=800)
|
|
145
|
+
>>> [a.distance for a in r.assignments], r.coverage_ratio
|
|
146
|
+
([500.0, 806.2257748298549], 0.6666666666666666)
|
|
147
|
+
"""
|
|
148
|
+
if radius < 0:
|
|
149
|
+
raise ValueError("Yarıçap negatif olamaz.")
|
|
150
|
+
if metric not in METRICS:
|
|
151
|
+
raise ValueError(f"Geçersiz metrik: {metric!r} ({', '.join(METRICS)})")
|
|
152
|
+
dist_fn = euclidean if metric == "euclidean" else haversine
|
|
153
|
+
out = CoverageResult(radius=float(radius), metric="matris" if distances else metric)
|
|
154
|
+
for d in demand:
|
|
155
|
+
best_id: Optional[str] = None
|
|
156
|
+
best: Optional[float] = None
|
|
157
|
+
for f in facilities:
|
|
158
|
+
if distances is not None:
|
|
159
|
+
dist = distances.get((d.id, f.id))
|
|
160
|
+
if dist is None:
|
|
161
|
+
continue
|
|
162
|
+
else:
|
|
163
|
+
dist = dist_fn((d.x, d.y), (f.x, f.y))
|
|
164
|
+
if best is None or dist < best:
|
|
165
|
+
best, best_id = float(dist), f.id
|
|
166
|
+
covered = best is not None and best <= radius + 1e-9
|
|
167
|
+
out.assignments.append(Assignment(d.id, best_id, best, covered, d.weight))
|
|
168
|
+
return out
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def read_points(
|
|
172
|
+
source: Union[PathLike, Mapping[str, Any]],
|
|
173
|
+
id_field: Optional[str] = None,
|
|
174
|
+
weight_field: Optional[str] = None,
|
|
175
|
+
filter_field: Optional[str] = None,
|
|
176
|
+
filter_values: Optional[Sequence[str]] = None,
|
|
177
|
+
x_field: str = "x",
|
|
178
|
+
y_field: str = "y",
|
|
179
|
+
) -> Tuple[List[Point], Optional[bool]]:
|
|
180
|
+
"""Read points from GeoJSON (any geometry; centroids used) or CSV with x/y columns.
|
|
181
|
+
|
|
182
|
+
Returns ``(points, geographic)`` where ``geographic`` is the auto-detected lon/lat flag
|
|
183
|
+
(``None`` for CSV input).
|
|
184
|
+
|
|
185
|
+
``filter_field``/``filter_values`` keep only features whose property matches one of the
|
|
186
|
+
values after :func:`tr_donati.landuse.normalize_key`, e.g. only "İlkokul" polygons from
|
|
187
|
+
a whole land-use layer.
|
|
188
|
+
"""
|
|
189
|
+
from .landuse import _to_float, normalize_key
|
|
190
|
+
|
|
191
|
+
wanted = {normalize_key(v) for v in filter_values} if filter_values else None
|
|
192
|
+
|
|
193
|
+
def keep(props: Mapping[str, Any]) -> bool:
|
|
194
|
+
if not filter_field or wanted is None:
|
|
195
|
+
return True
|
|
196
|
+
return normalize_key(props.get(filter_field, "")) in wanted
|
|
197
|
+
|
|
198
|
+
pts: List[Point] = []
|
|
199
|
+
if not isinstance(source, Mapping) and Path(source).suffix.lower() in (".csv", ".txt"):
|
|
200
|
+
text = Path(source).read_text(encoding="utf-8-sig")
|
|
201
|
+
from .landuse import _sniff
|
|
202
|
+
|
|
203
|
+
for i, row in enumerate(csv.DictReader(io.StringIO(text), dialect=_sniff(text))):
|
|
204
|
+
if not keep(row):
|
|
205
|
+
continue
|
|
206
|
+
x, y = _to_float(row.get(x_field)), _to_float(row.get(y_field))
|
|
207
|
+
if x is None or y is None:
|
|
208
|
+
raise ValueError(f"{i + 2}. satırda geçersiz koordinat.")
|
|
209
|
+
w = _to_float(row.get(weight_field)) if weight_field else None
|
|
210
|
+
pid = row.get(id_field) if id_field else None
|
|
211
|
+
pts.append(Point(str(pid or i), x, y, 1.0 if w is None else w))
|
|
212
|
+
return pts, None
|
|
213
|
+
data: Any = (
|
|
214
|
+
source
|
|
215
|
+
if isinstance(source, Mapping)
|
|
216
|
+
else json.loads(Path(source).read_text(encoding="utf-8-sig"))
|
|
217
|
+
)
|
|
218
|
+
feats = list(data.get("features", [])) if data.get("type") != "Feature" else [data]
|
|
219
|
+
feats = [f for f in feats if keep(f.get("properties") or {})]
|
|
220
|
+
geographic = looks_geographic(f.get("geometry") for f in feats) if feats else None
|
|
221
|
+
for i, f in enumerate(feats):
|
|
222
|
+
props = f.get("properties") or {}
|
|
223
|
+
c = centroid(f.get("geometry"))
|
|
224
|
+
if c is None:
|
|
225
|
+
continue
|
|
226
|
+
w = _to_float(props.get(weight_field)) if weight_field else None
|
|
227
|
+
pid = props.get(id_field) if id_field else f.get("id")
|
|
228
|
+
pts.append(Point(str(pid if pid is not None else i), c[0], c[1], 1.0 if w is None else w))
|
|
229
|
+
return pts, geographic
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def read_distance_matrix(path: PathLike) -> Dict[Tuple[str, str], float]:
|
|
233
|
+
"""Read a long-format distance CSV with columns ``talep``/``origin``, ``tesis``/
|
|
234
|
+
``destination`` and ``mesafe``/``distance``."""
|
|
235
|
+
from .landuse import _sniff, _to_float, normalize_key
|
|
236
|
+
|
|
237
|
+
text = Path(path).read_text(encoding="utf-8-sig")
|
|
238
|
+
out: Dict[Tuple[str, str], float] = {}
|
|
239
|
+
for i, row in enumerate(csv.DictReader(io.StringIO(text), dialect=_sniff(text))):
|
|
240
|
+
r = {normalize_key(k): (v or "").strip() for k, v in row.items() if k}
|
|
241
|
+
o = r.get("talep") or r.get("origin")
|
|
242
|
+
d = r.get("tesis") or r.get("destination")
|
|
243
|
+
dist = _to_float(r.get("mesafe") or r.get("distance"))
|
|
244
|
+
if not o or not d or dist is None:
|
|
245
|
+
raise ValueError(f"Mesafe CSV'sinin {i + 2}. satırı eksik veya hatalı.")
|
|
246
|
+
out[(o, d)] = dist
|
|
247
|
+
return out
|
tr_donati/cli.py
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"""Komut satırı arayüzü: ``tr-donati``.
|
|
2
|
+
|
|
3
|
+
Alt komutlar:
|
|
4
|
+
|
|
5
|
+
* ``standartlar`` - EK-2 standart tablosunu yazdırır.
|
|
6
|
+
* ``ihtiyac`` - planlanan nüfus için gerekli donatı alanları.
|
|
7
|
+
* ``denetle`` - GeoJSON/CSV plan arazi kullanımını standartlarla karşılaştırır.
|
|
8
|
+
* ``erisim`` - tesis noktalarının hizmet yarıçapı (etki alanı) kontrolü.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import json
|
|
15
|
+
import sys
|
|
16
|
+
from typing import List, Optional, Sequence
|
|
17
|
+
|
|
18
|
+
from ._version import __version__
|
|
19
|
+
from .access import (
|
|
20
|
+
default_service_radius,
|
|
21
|
+
read_distance_matrix,
|
|
22
|
+
read_points,
|
|
23
|
+
service_coverage,
|
|
24
|
+
)
|
|
25
|
+
from .compliance import check_compliance
|
|
26
|
+
from .landuse import LandUseMapping, read_plan, summarize
|
|
27
|
+
from .report import (
|
|
28
|
+
requirements_csv,
|
|
29
|
+
requirements_markdown,
|
|
30
|
+
standards_markdown,
|
|
31
|
+
)
|
|
32
|
+
from .requirements import required_areas
|
|
33
|
+
from .standards import StandardsError, StandardsTable
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _write(text: str, output: Optional[str]) -> None:
|
|
37
|
+
if output:
|
|
38
|
+
with open(output, "w", encoding="utf-8", newline="\n") as fh:
|
|
39
|
+
fh.write(text)
|
|
40
|
+
print(f"Rapor yazıldı: {output}", file=sys.stderr)
|
|
41
|
+
else:
|
|
42
|
+
sys.stdout.write(text)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _table(args: argparse.Namespace) -> StandardsTable:
|
|
46
|
+
if getattr(args, "standards", None):
|
|
47
|
+
return StandardsTable.from_json(args.standards)
|
|
48
|
+
return StandardsTable.default()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _geo_flag(args: argparse.Namespace) -> Optional[bool]:
|
|
52
|
+
if getattr(args, "geographic", False):
|
|
53
|
+
return True
|
|
54
|
+
if getattr(args, "projected", False):
|
|
55
|
+
return False
|
|
56
|
+
return None
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _add_common(p: argparse.ArgumentParser) -> None:
|
|
60
|
+
p.add_argument(
|
|
61
|
+
"--standartlar",
|
|
62
|
+
dest="standards",
|
|
63
|
+
metavar="JSON",
|
|
64
|
+
help="Standart tablosunu değiştiren JSON dosyası (varsayılan tabloyla birleştirilir).",
|
|
65
|
+
)
|
|
66
|
+
p.add_argument("-o", "--cikti", dest="output", metavar="DOSYA", help="Çıktı dosyası.")
|
|
67
|
+
p.add_argument(
|
|
68
|
+
"--bicim",
|
|
69
|
+
dest="fmt",
|
|
70
|
+
choices=("md", "csv"),
|
|
71
|
+
default="md",
|
|
72
|
+
help="Çıktı biçimi: md (Markdown, varsayılan) veya csv.",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _add_population(p: argparse.ArgumentParser) -> None:
|
|
77
|
+
p.add_argument(
|
|
78
|
+
"--nufus", dest="population", type=float, required=True, help="Planlanan nüfus (kişi)."
|
|
79
|
+
)
|
|
80
|
+
p.add_argument(
|
|
81
|
+
"--yerlesme-nufusu",
|
|
82
|
+
dest="settlement_population",
|
|
83
|
+
type=float,
|
|
84
|
+
help="Nüfus grubunu belirleyen ilçe projeksiyon nüfusu (EK-2 açıklama 1). "
|
|
85
|
+
"Verilmezse planlanan nüfus kullanılır.",
|
|
86
|
+
)
|
|
87
|
+
p.add_argument(
|
|
88
|
+
"--kapsam",
|
|
89
|
+
dest="scope",
|
|
90
|
+
choices=("ilce", "il"),
|
|
91
|
+
default="ilce",
|
|
92
|
+
help="ilce: ilçe sınırları dahilinde (varsayılan); il: il bütünü (+5 m²/kişi yeşil).",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
97
|
+
"""argparse parser for ``tr-donati``."""
|
|
98
|
+
parser = argparse.ArgumentParser(
|
|
99
|
+
prog="tr-donati",
|
|
100
|
+
description="Mekânsal Planlar Yapım Yönetmeliği EK-2'ye göre donatı alanı ihtiyacı, "
|
|
101
|
+
"plan uygunluk denetimi ve erişilebilirlik analizi.",
|
|
102
|
+
)
|
|
103
|
+
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
104
|
+
sub = parser.add_subparsers(dest="command", metavar="KOMUT")
|
|
105
|
+
|
|
106
|
+
p = sub.add_parser("standartlar", help="EK-2 standart tablosunu yazdırır.")
|
|
107
|
+
p.add_argument("--standartlar", dest="standards", metavar="JSON")
|
|
108
|
+
p.add_argument("--json", action="store_true", help="Tabloyu JSON olarak yazdır.")
|
|
109
|
+
p.add_argument("-o", "--cikti", dest="output", metavar="DOSYA")
|
|
110
|
+
|
|
111
|
+
p = sub.add_parser("ihtiyac", help="Planlanan nüfus için gerekli donatı alanları.")
|
|
112
|
+
_add_population(p)
|
|
113
|
+
p.add_argument(
|
|
114
|
+
"--tumu",
|
|
115
|
+
dest="include_all",
|
|
116
|
+
action="store_true",
|
|
117
|
+
help="Standardı olmayan kategorileri de (otopark, resmi kurum) listele.",
|
|
118
|
+
)
|
|
119
|
+
_add_common(p)
|
|
120
|
+
|
|
121
|
+
p = sub.add_parser("denetle", help="Plan arazi kullanımını standartlarla karşılaştırır.")
|
|
122
|
+
p.add_argument("girdi", help="Plan arazi kullanımı: .geojson/.json veya .csv")
|
|
123
|
+
_add_population(p)
|
|
124
|
+
p.add_argument(
|
|
125
|
+
"--kod-alani",
|
|
126
|
+
dest="code_field",
|
|
127
|
+
required=True,
|
|
128
|
+
help="Arazi kullanım adını/kodunu içeren alan (sütun).",
|
|
129
|
+
)
|
|
130
|
+
p.add_argument(
|
|
131
|
+
"--alan-alani",
|
|
132
|
+
dest="area_field",
|
|
133
|
+
help="Alanı (m²) içeren alan. GeoJSON'da verilmezse geometriden hesaplanır.",
|
|
134
|
+
)
|
|
135
|
+
p.add_argument("--kimlik-alani", dest="id_field", help="Parsel kimliği alanı.")
|
|
136
|
+
p.add_argument(
|
|
137
|
+
"--birim-alani",
|
|
138
|
+
dest="units_field",
|
|
139
|
+
help="Yatak/ünit sayısı alanı (hastane, ağız-diş sağlığı merkezi parsel kontrolü).",
|
|
140
|
+
)
|
|
141
|
+
p.add_argument(
|
|
142
|
+
"--eslestirme",
|
|
143
|
+
dest="mapping",
|
|
144
|
+
metavar="DOSYA",
|
|
145
|
+
help="Kullanım kodu -> donatı türü eşleştirmesi (JSON veya CSV). Varsayılanı genişletir.",
|
|
146
|
+
)
|
|
147
|
+
g = p.add_mutually_exclusive_group()
|
|
148
|
+
g.add_argument(
|
|
149
|
+
"--cografi", dest="geographic", action="store_true", help="Koordinatlar boylam/enlem."
|
|
150
|
+
)
|
|
151
|
+
g.add_argument(
|
|
152
|
+
"--projeksiyonlu",
|
|
153
|
+
dest="projected",
|
|
154
|
+
action="store_true",
|
|
155
|
+
help="Koordinatlar metrik projeksiyonda.",
|
|
156
|
+
)
|
|
157
|
+
p.add_argument("--baslik", dest="title", help="Rapor başlığı.")
|
|
158
|
+
p.add_argument(
|
|
159
|
+
"--siki",
|
|
160
|
+
dest="strict",
|
|
161
|
+
action="store_true",
|
|
162
|
+
help="Standart sağlanmazsa çıkış kodu 1 döndür.",
|
|
163
|
+
)
|
|
164
|
+
_add_common(p)
|
|
165
|
+
|
|
166
|
+
p = sub.add_parser("erisim", help="Hizmet yarıçapı (etki alanı) kontrolü.")
|
|
167
|
+
p.add_argument("--tesisler", required=True, help="Tesis noktaları/poligonları (GeoJSON/CSV).")
|
|
168
|
+
p.add_argument("--talep", required=True, help="Talep noktaları (GeoJSON/CSV).")
|
|
169
|
+
p.add_argument("--yaricap", dest="radius", type=float, help="Hizmet yarıçapı (m).")
|
|
170
|
+
p.add_argument(
|
|
171
|
+
"--tur",
|
|
172
|
+
dest="facility",
|
|
173
|
+
help="Tesis türü (örn. ilkokul); --yaricap verilmezse gösterge değer kullanılır.",
|
|
174
|
+
)
|
|
175
|
+
p.add_argument("--agirlik-alani", dest="weight_field", help="Talep ağırlığı (örn. nüfus).")
|
|
176
|
+
p.add_argument("--talep-kimlik-alani", dest="demand_id_field")
|
|
177
|
+
p.add_argument("--tesis-kimlik-alani", dest="facility_id_field")
|
|
178
|
+
p.add_argument(
|
|
179
|
+
"--tesis-filtre-alani",
|
|
180
|
+
dest="filter_field",
|
|
181
|
+
help="Tesis katmanında süzgeç alanı (örn. KULLANIM).",
|
|
182
|
+
)
|
|
183
|
+
p.add_argument(
|
|
184
|
+
"--tesis-filtre-degeri",
|
|
185
|
+
dest="filter_values",
|
|
186
|
+
action="append",
|
|
187
|
+
help="Süzgeç değeri; birden çok kez verilebilir (örn. 'İlkokul').",
|
|
188
|
+
)
|
|
189
|
+
p.add_argument(
|
|
190
|
+
"--talep-filtre-alani",
|
|
191
|
+
dest="demand_filter_field",
|
|
192
|
+
help="Talep katmanında süzgeç alanı (örn. KULLANIM).",
|
|
193
|
+
)
|
|
194
|
+
p.add_argument(
|
|
195
|
+
"--talep-filtre-degeri",
|
|
196
|
+
dest="demand_filter_values",
|
|
197
|
+
action="append",
|
|
198
|
+
help="Talep süzgeç değeri (örn. 'Konut').",
|
|
199
|
+
)
|
|
200
|
+
p.add_argument(
|
|
201
|
+
"--mesafeler",
|
|
202
|
+
dest="distances",
|
|
203
|
+
help="Hazır mesafe matrisi CSV (talep, tesis, mesafe) - örn. ağ mesafeleri.",
|
|
204
|
+
)
|
|
205
|
+
g = p.add_mutually_exclusive_group()
|
|
206
|
+
g.add_argument("--cografi", dest="geographic", action="store_true")
|
|
207
|
+
g.add_argument("--projeksiyonlu", dest="projected", action="store_true")
|
|
208
|
+
p.add_argument("--baslik", dest="title")
|
|
209
|
+
p.add_argument("-o", "--cikti", dest="output", metavar="DOSYA")
|
|
210
|
+
p.add_argument("--bicim", dest="fmt", choices=("md", "csv"), default="md")
|
|
211
|
+
return parser
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _cmd_standards(args: argparse.Namespace) -> int:
|
|
215
|
+
t = _table(args)
|
|
216
|
+
if args.json:
|
|
217
|
+
_write(json.dumps(t.to_dict(), ensure_ascii=False, indent=2) + "\n", args.output)
|
|
218
|
+
else:
|
|
219
|
+
_write(standards_markdown(t), args.output)
|
|
220
|
+
return 0
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _cmd_requirements(args: argparse.Namespace) -> int:
|
|
224
|
+
t = _table(args)
|
|
225
|
+
reqs = required_areas(
|
|
226
|
+
args.population,
|
|
227
|
+
settlement_population=args.settlement_population,
|
|
228
|
+
scope=args.scope,
|
|
229
|
+
table=t,
|
|
230
|
+
include_unstandardised=args.include_all,
|
|
231
|
+
)
|
|
232
|
+
if args.fmt == "csv":
|
|
233
|
+
_write(requirements_csv(reqs), args.output)
|
|
234
|
+
else:
|
|
235
|
+
_write(
|
|
236
|
+
requirements_markdown(
|
|
237
|
+
reqs, args.population, args.settlement_population, args.scope, table=t
|
|
238
|
+
),
|
|
239
|
+
args.output,
|
|
240
|
+
)
|
|
241
|
+
return 0
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _cmd_check(args: argparse.Namespace) -> int:
|
|
245
|
+
t = _table(args)
|
|
246
|
+
mapping = (
|
|
247
|
+
LandUseMapping.from_file(args.mapping, table=t)
|
|
248
|
+
if args.mapping
|
|
249
|
+
else LandUseMapping.default(t)
|
|
250
|
+
)
|
|
251
|
+
feats = read_plan(
|
|
252
|
+
args.girdi,
|
|
253
|
+
args.code_field,
|
|
254
|
+
area_field=args.area_field,
|
|
255
|
+
id_field=args.id_field,
|
|
256
|
+
geographic=_geo_flag(args),
|
|
257
|
+
)
|
|
258
|
+
summary = summarize(feats, mapping, units_field=args.units_field)
|
|
259
|
+
rep = check_compliance(
|
|
260
|
+
summary.provided,
|
|
261
|
+
args.population,
|
|
262
|
+
settlement_population=args.settlement_population,
|
|
263
|
+
scope=args.scope,
|
|
264
|
+
table=t,
|
|
265
|
+
parcels=summary.parcels,
|
|
266
|
+
unmapped=summary.unmapped,
|
|
267
|
+
plan_area=summary.plan_area,
|
|
268
|
+
)
|
|
269
|
+
_write(rep.to_csv() if args.fmt == "csv" else rep.to_markdown(args.title), args.output)
|
|
270
|
+
return 1 if args.strict and not rep.is_compliant else 0
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _cmd_access(args: argparse.Namespace) -> int:
|
|
274
|
+
radius = args.radius
|
|
275
|
+
if radius is None:
|
|
276
|
+
radius = default_service_radius(args.facility) if args.facility else None
|
|
277
|
+
if radius is None:
|
|
278
|
+
raise StandardsError(
|
|
279
|
+
"Hizmet yarıçapı bulunamadı: --yaricap verin (yönetmelikte yarıçap yoktur)."
|
|
280
|
+
)
|
|
281
|
+
print(
|
|
282
|
+
f"Uyarı: '{args.facility}' için gösterge yarıçap {radius:g} m kullanıldı "
|
|
283
|
+
"(yönetmelik değeri değildir, doğrulanmamıştır).",
|
|
284
|
+
file=sys.stderr,
|
|
285
|
+
)
|
|
286
|
+
facilities, geo_f = read_points(
|
|
287
|
+
args.tesisler,
|
|
288
|
+
id_field=args.facility_id_field,
|
|
289
|
+
filter_field=args.filter_field,
|
|
290
|
+
filter_values=args.filter_values,
|
|
291
|
+
)
|
|
292
|
+
demand, geo_d = read_points(
|
|
293
|
+
args.talep,
|
|
294
|
+
id_field=args.demand_id_field,
|
|
295
|
+
weight_field=args.weight_field,
|
|
296
|
+
filter_field=args.demand_filter_field,
|
|
297
|
+
filter_values=args.demand_filter_values,
|
|
298
|
+
)
|
|
299
|
+
geo = _geo_flag(args)
|
|
300
|
+
if geo is None:
|
|
301
|
+
geo = bool(geo_f) and bool(geo_d)
|
|
302
|
+
distances = read_distance_matrix(args.distances) if args.distances else None
|
|
303
|
+
res = service_coverage(
|
|
304
|
+
demand,
|
|
305
|
+
facilities,
|
|
306
|
+
radius,
|
|
307
|
+
metric="haversine" if geo else "euclidean",
|
|
308
|
+
distances=distances,
|
|
309
|
+
)
|
|
310
|
+
_write(res.to_csv() if args.fmt == "csv" else res.to_markdown(args.title), args.output)
|
|
311
|
+
return 0
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def main(argv: Optional[Sequence[str]] = None) -> int:
|
|
315
|
+
"""Entry point; returns the process exit code."""
|
|
316
|
+
reconfigure = getattr(sys.stdout, "reconfigure", None)
|
|
317
|
+
if reconfigure is not None and (sys.stdout.encoding or "").lower() != "utf-8":
|
|
318
|
+
try: # Turkish characters and "m²" must survive redirection on Windows.
|
|
319
|
+
reconfigure(encoding="utf-8")
|
|
320
|
+
except (OSError, ValueError): # pragma: no cover
|
|
321
|
+
pass
|
|
322
|
+
parser = build_parser()
|
|
323
|
+
args = parser.parse_args(list(argv) if argv is not None else None)
|
|
324
|
+
if not args.command:
|
|
325
|
+
parser.print_help()
|
|
326
|
+
return 2
|
|
327
|
+
handlers = {
|
|
328
|
+
"standartlar": _cmd_standards,
|
|
329
|
+
"ihtiyac": _cmd_requirements,
|
|
330
|
+
"denetle": _cmd_check,
|
|
331
|
+
"erisim": _cmd_access,
|
|
332
|
+
}
|
|
333
|
+
try:
|
|
334
|
+
return handlers[args.command](args)
|
|
335
|
+
except (StandardsError, ValueError, KeyError, OSError) as exc:
|
|
336
|
+
msg: List[str] = [f"Hata: {exc}"]
|
|
337
|
+
print("\n".join(msg), file=sys.stderr)
|
|
338
|
+
return 2
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
if __name__ == "__main__": # pragma: no cover
|
|
342
|
+
sys.exit(main())
|