-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds stats and transliterations for custom exports in GeoJSON format + HDX customviz #289
Merged
kshitijrajsharma
merged 5 commits into
develop
from
feature/post-processing-stats-translit
Jan 27, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf10476
+ Post-processing GeoJSON datasets adding stats and transliterations
emi420 2a4a362
Add missing railways stats html template
emi420 60790df
Add geojsonstats req
emi420 f60349b
Black reformatting
emi420 e3066b6
Missing requirements
emi420 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from geojson_stats.stats import Stats | ||
from geojson_stats.html import Html | ||
|
||
CONFIG_AREA = ["building"] | ||
CONFIG_LENGTH = ["highway", "waterway"] | ||
|
||
|
||
class GeoJSONStats(Stats): | ||
"""Used for collecting stats while processing GeoJSON files line by line""" | ||
|
||
def __init__(self, filters, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.config.clean = True | ||
self.config.properties_prop = "properties.tags" | ||
|
||
if filters and filters.tags: | ||
for tag in CONFIG_AREA: | ||
if self.check_filter(filters.tags, tag): | ||
self.config.keys.append(tag) | ||
self.config.value_keys.append(tag) | ||
self.config.area = True | ||
|
||
for tag in CONFIG_LENGTH: | ||
if self.check_filter(filters.tags, tag): | ||
self.config.keys.append(tag) | ||
self.config.value_keys.append(tag) | ||
self.config.length = True | ||
|
||
def check_filter(self, tags, tag): | ||
""" | ||
Check if a tag is present in tag filters | ||
""" | ||
|
||
if tags.all_geometry: | ||
if tags.all_geometry.join_or and tag in tags.all_geometry.join_or: | ||
return True | ||
if tags.all_geometry.join_and and tag in tags.all_geometry.join_and: | ||
return True | ||
if tags.polygon: | ||
if tags.polygon.join_or and tag in tags.polygon.join_or: | ||
return True | ||
if tags.polygon.join_and and tag in tags.polygon.join_and: | ||
return True | ||
if tags.line: | ||
if tags.line.join_or and tag in tags.line.join_or: | ||
return True | ||
if tags.line.join_and and tag in tags.line.join_and: | ||
return True | ||
|
||
def raw_data_line_stats(self, json_object: dict): | ||
""" | ||
Process a GeoJSON line (for getting stats) and return that line | ||
""" | ||
self.get_object_stats(json_object) | ||
|
||
def html(self, tpl): | ||
""" | ||
Returns stats Html object, generated from stats data using a template | ||
""" | ||
return Html(tpl, self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import json | ||
from .transliterator import Transliterator | ||
from .geojson_stats import GeoJSONStats | ||
import os | ||
import pathlib | ||
|
||
|
||
class PostProcessor: | ||
"""Used for posst-process data while processing GeoJSON files line by line""" | ||
|
||
options = {} | ||
filters = {} | ||
functions = [] | ||
|
||
def __init__(self, options, *args, **kwargs): | ||
self.options = options | ||
|
||
def post_process_line(self, line: str): | ||
""" | ||
Parses line, run functions over it and returns it | ||
""" | ||
|
||
line_object = json.loads(line) | ||
|
||
for fn in self.functions: | ||
fn(line_object) | ||
|
||
return json.dumps(line_object) | ||
|
||
def custom( | ||
self, category_name, export_format_path, export_filename, file_export_path | ||
): | ||
""" | ||
Post-process custom exports | ||
""" | ||
self.geoJSONStats.config.properties_prop = "properties" | ||
|
||
category_tag = "" | ||
if category_name == "roads": | ||
category_tag = "highway" | ||
self.geoJSONStats.config.length = True | ||
elif category_name == "buildings": | ||
category_tag = "building" | ||
self.geoJSONStats.config.area = True | ||
elif category_name == "waterways": | ||
category_tag = "waterway" | ||
self.geoJSONStats.config.length = True | ||
elif category_name == "railways": | ||
category_tag = "railway" | ||
self.geoJSONStats.config.length = True | ||
|
||
if self.options["include_stats"]: | ||
if category_tag: | ||
self.geoJSONStats.config.keys.append(category_tag) | ||
self.geoJSONStats.config.value_keys.append(category_tag) | ||
|
||
path_input = os.path.join(export_format_path, f"{export_filename}.geojson") | ||
path_output = os.path.join( | ||
export_format_path, f"{export_filename}-post.geojson" | ||
) | ||
|
||
with open(path_input, "r") as input_file, open( | ||
path_output, "w" | ||
) as output_file: | ||
for line in input_file: | ||
comma = False | ||
if line.startswith('{ "type": "Feature"'): | ||
json_string = "" | ||
if line[-2:-1] == ",": | ||
json_string = line[:-2] | ||
comma = True | ||
else: | ||
json_string = line | ||
line = self.post_process_line(json_string) | ||
if self.options["include_translit"]: | ||
if comma: | ||
output_file.write(line + ",") | ||
else: | ||
output_file.write(line) | ||
|
||
if self.options.get("include_translit"): | ||
os.remove(path_input) | ||
os.rename(path_output, path_input) | ||
else: | ||
os.remove(path_output) | ||
|
||
geojson_stats_json = json.dumps(self.geoJSONStats.dict()) | ||
with open( | ||
os.path.join(file_export_path, "stats.json"), | ||
"w", | ||
) as f: | ||
f.write(geojson_stats_json) | ||
|
||
if self.options.get("include_stats_html"): | ||
tpl = ( | ||
"stats_{category_tag}".format(category_tag=category_tag) | ||
if category_tag | ||
else "stats" | ||
) | ||
project_root = pathlib.Path(__file__).resolve().parent | ||
tpl_path = os.path.join( | ||
project_root, | ||
"{tpl}_tpl.html".format(tpl=tpl), | ||
) | ||
geojson_stats_html = self.geoJSONStats.html(tpl_path).build() | ||
upload_html_path = os.path.join(file_export_path, "stats-summary.html") | ||
with open(upload_html_path, "w") as f: | ||
f.write(geojson_stats_html) | ||
|
||
def init(self): | ||
""" | ||
Initialize post-processor | ||
""" | ||
|
||
if self.options.get("include_stats"): | ||
self.geoJSONStats = GeoJSONStats(self.filters) | ||
self.functions.append(self.geoJSONStats.raw_data_line_stats) | ||
|
||
if self.options.get("include_translit"): | ||
self.transliterator = Transliterator() | ||
self.functions.append(self.transliterator.translit) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Need for this