Skip to content

Commit

Permalink
Add support for nested properties, ex: properties.tags)
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed Oct 23, 2024
1 parent b32deee commit 3ce32d2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ __pypackages__

# System files
.DS_Store

# Virtual envs
venv
10 changes: 6 additions & 4 deletions geojson_stats/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ def main():
args.add_argument("--url", "-u", help="URL of GeoJSON file to analyze", type=str, default=None)
args.add_argument("--verbose", "-v", help="Verbose", default=False, action='store_true')
args.add_argument("--stream", help="Stream a file (use less memory)", default=False, action='store_true')
args.add_argument("--keys", help="Keys for calculating length or area", default = None)
args.add_argument("--value-keys", help="Keys for counting values", default = None)
args.add_argument("--keys", help="Keys for calculating length or area", type=str, default = None)
args.add_argument("--value-keys", help="Keys for counting values", type=str, default = None)
args.add_argument("--length", help="Calculate total length of all linestrings", \
default=False, action='store_true')
args.add_argument("--area", help="Calculate total area of all polygons", \
default=False, action='store_true')
args.add_argument("--projected", help="Use projected coordinated in meters", \
default=False, action='store_true')
args.add_argument("--proj", help="Data projection system", default = "WGS84")
args.add_argument("--proj", help="Data projection system", type=str, default = "WGS84")
args.add_argument("--no-clean", help="Keep zero and empty results", default=False, action='store_true')
args.add_argument("--properties-prop", help="Properties to analyze (ex: properties or properties.tags)", type=str, default="properties")
args = args.parse_args()

config = Config(
Expand All @@ -33,7 +34,8 @@ def main():
value_keys = args.value_keys,
projected = args.projected,
clean = not args.no_clean,
proj = args.proj
proj = args.proj,
properties_prop = args.properties_prop
)
stats = Stats(config)

Expand Down
3 changes: 3 additions & 0 deletions geojson_stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Config:
clean: bool = False
# Coordinates system
proj: str = "WGS84"
# Properties to analyze (ex: "properties" or "properties.tags")
properties_prop: str = "properties"


# Base stats class
@dataclass
Expand Down
14 changes: 13 additions & 1 deletion geojson_stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ def __init__(self, config: Config = None):
projected=self.config.projected
)

# Get property from tag
def getProperty(self, path: str, obj: dict):
keys = path.split('.')
value = obj

for key in keys:
value = value.get(key)
if value is None:
return None

return value

# Clean calculation cache
def clean_cache(self):
self.cache = self.cache = CalcCache()
Expand Down Expand Up @@ -77,7 +89,7 @@ def total_keys(self, key: str):

# Get stats for a Feature object
def get_object_stats(self, json_object: object):
for prop in json_object["properties"].items():
for prop in self.getProperty(self.config.properties_prop, json_object).items():
if prop[1]:
self.total_keys(prop[0])

Expand Down
Empty file removed tests/__init__.py
Empty file.

0 comments on commit 3ce32d2

Please sign in to comment.