Skip to content

Commit

Permalink
Merge pull request #421 from OpenDataServices/419-wkt
Browse files Browse the repository at this point in the history
create_template: Create a template suitable for WKT data
  • Loading branch information
Bjwebb authored Jun 27, 2023
2 parents a3627ae + 09e3142 commit 32317fa
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.22.0] - 2023-06-27

### Added

- Generate templates that are correct for WKT <-> geojson conversion

## [0.21.0] - 2023-06-23

### Added
Expand Down
2 changes: 2 additions & 0 deletions examples/help/create-template/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ usage: flatten-tool create-template [-h] -s SCHEMA [-f {csv,ods,xlsx,all}]
[--no-deprecated-fields]
[--truncation-length TRUNCATION_LENGTH]
[--line-terminator LINE_TERMINATOR]
[--convert-wkt]

optional arguments:
-h, --help show this help message and exit
Expand Down Expand Up @@ -36,3 +37,4 @@ optional arguments:
--line-terminator LINE_TERMINATOR
The line terminator to use when writing CSV files:
CRLF or LF
--convert-wkt Enable conversion of WKT to geojson
1 change: 1 addition & 0 deletions examples/wkt/create_template/cmd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ flatten-tool create-template --convert-wkt --schema examples/wkt/create_template/schema.json -o examples/wkt/create_template/actual
1 change: 1 addition & 0 deletions examples/wkt/create_template/expected/main.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
geo
11 changes: 11 additions & 0 deletions examples/wkt/create_template/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"properties": {
"geo": {
"type": "object",
"properties": {
"type": {},
"coordinates": {}
}
}
}
}
9 changes: 8 additions & 1 deletion flattentool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def create_template(
truncation_length=3,
no_deprecated_fields=False,
line_terminator="CRLF",
convert_wkt=False,
**_,
):
"""
Expand All @@ -37,6 +38,8 @@ def create_template(
if line_terminator not in LINE_TERMINATORS.keys():
raise Exception(f"{line_terminator} is not a valid line terminator")

convert_flags = {"wkt": convert_wkt}

parser = SchemaParser(
schema_filename=schema,
rollup=rollup,
Expand All @@ -45,6 +48,7 @@ def create_template(
disable_local_refs=disable_local_refs,
truncation_length=truncation_length,
exclude_deprecated_fields=no_deprecated_fields,
convert_flags=convert_flags,
)
parser.parse()

Expand Down Expand Up @@ -121,6 +125,7 @@ def flatten(
use_titles=use_titles,
disable_local_refs=disable_local_refs,
truncation_length=truncation_length,
convert_flags=convert_flags,
)
schema_parser.parse()
else:
Expand Down Expand Up @@ -270,7 +275,9 @@ def unflatten(
)
if metatab_schema:
parser = SchemaParser(
schema_filename=metatab_schema, disable_local_refs=disable_local_refs
schema_filename=metatab_schema,
disable_local_refs=disable_local_refs,
convert_flags=convert_flags,
)
parser.parse()
spreadsheet_input.parser = parser
Expand Down
5 changes: 5 additions & 0 deletions flattentool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def create_parser():
"--line-terminator",
help="The line terminator to use when writing CSV files: CRLF or LF",
)
parser_create_template.add_argument(
"--convert-wkt",
action="store_true",
help="Enable conversion of WKT to geojson",
)

parser_flatten = subparsers.add_parser("flatten", help="Flatten a JSON file")
parser_flatten.add_argument("input_name", help="Name of the input JSON file.")
Expand Down
16 changes: 10 additions & 6 deletions flattentool/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,6 @@ def parse_schema_dict(
yield (field, child_title)

elif "properties" in schema_dict:
if (
self.convert_flags.get("wkt")
and "type" in schema_dict["properties"]
and "coordinates" in schema_dict["properties"]
):
self.flattened[parent_path.replace("/0/", "/").strip("/")] = "geojson"
if "id" in schema_dict["properties"]:
if self.use_titles:
id_fields = parent_id_fields + [
Expand Down Expand Up @@ -271,6 +265,16 @@ def parse_schema_dict(
title_lookup[title].property_name = property_name

if "object" in property_type_set:
if (
self.convert_flags.get("wkt")
and "type" in property_schema_dict.get("properties", {})
and "coordinates" in property_schema_dict.get("properties", {})
):
self.flattened[
parent_path.replace("/0/", "/") + property_name
] = "geojson"
yield (property_name, title)
continue
self.flattened[parent_path + property_name] = "object"
for field, child_title in self.parse_schema_dict(
parent_path + property_name,
Expand Down
2 changes: 1 addition & 1 deletion flattentool/tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_example_in_doc(root, filename):


def test_expected_number_of_examples_in_docs_data():
expected = 66
expected = 67
# See _get_examples_in_docs_data()
if sys.version_info[:2] != (3, 8):
expected -= 3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run(self):

setup(
name="flattentool",
version="0.21.0",
version="0.22.0",
author="Open Data Services",
author_email="[email protected]",
packages=["flattentool"],
Expand Down

0 comments on commit 32317fa

Please sign in to comment.