From ef4371c3c3257f28c9f8d4adad11834fe9593ff7 Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Tue, 25 Jun 2019 14:43:12 +0200 Subject: [PATCH 1/7] added conversion script --- geojson2csv/geojson2csv.py | 34 ++++++++++++++++++++++++++++++++++ geojson2csv/samples.geojson | 10 ++++++++++ 2 files changed, 44 insertions(+) create mode 100644 geojson2csv/geojson2csv.py create mode 100644 geojson2csv/samples.geojson diff --git a/geojson2csv/geojson2csv.py b/geojson2csv/geojson2csv.py new file mode 100644 index 0000000..ddb72d4 --- /dev/null +++ b/geojson2csv/geojson2csv.py @@ -0,0 +1,34 @@ +import argparse +import csv +import json +import sys +import geojson +from shapely.geometry import shape + +def parse_geojson(infile, outfile, geometry_column_name): + with open(infile, 'r') as geojsonfile: + input_geojson = geojson.load(geojsonfile) + + records = [] + for elm in input_geojson.features: + geom = shape(elm['geometry']).wkt + geom = {geometry_column_name: geom} + prop = elm['properties'] + prop.update(geom) + records.append(prop) + + with open(outfile, 'w') as csvfile: + fieldnames = records[0].keys() + writer = csv.DictWriter(csvfile, fieldnames) + + writer.writeheader() + writer.writerows(records) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Convert geojson to csv with WKT geometries.') + parser.add_argument('infile', help="Input geojson file name") + parser.add_argument('outfile', help="Output csv file name") + parser.add_argument('--colname', default='geometry', help="Geometry column name") + args = parser.parse_args() + + parse_geojson(args.infile, args.outfile, args.colname) \ No newline at end of file diff --git a/geojson2csv/samples.geojson b/geojson2csv/samples.geojson new file mode 100644 index 0000000..34b06e1 --- /dev/null +++ b/geojson2csv/samples.geojson @@ -0,0 +1,10 @@ +{ +"type": "FeatureCollection", +"name": "samples", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, +"features": [ +{ "type": "Feature", "properties": { "Feature 1": 1, "Feature 2": "AAA" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.291400058607934, 37.749843712357368 ], [ -122.341812276949355, 37.737194828482615 ], [ -122.324947098449684, 37.67926660667937 ], [ -122.252536821195633, 37.680733143940216 ], [ -122.291400058607934, 37.749843712357368 ] ] ] } }, +{ "type": "Feature", "properties": { "Feature 1": 2, "Feature 2": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.605788983900837, 37.747827223623695 ], [ -122.65858432529113, 37.683299584146667 ], [ -122.519996554141599, 37.656168644821101 ], [ -122.513397136467816, 37.725829164711072 ], [ -122.605788983900837, 37.747827223623695 ] ] ] } }, +{ "type": "Feature", "properties": { "Feature 1": 3, "Feature 2": "CCCCC" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.459135257816683, 38.084397524986819 ], [ -122.584524193618634, 38.028669109074848 ], [ -122.466467944120893, 37.96414146959782 ], [ -122.350611500514404, 38.05066716798747 ], [ -122.35281130640567, 38.103462509377763 ], [ -122.459135257816683, 38.084397524986819 ] ] ] } } +] +} From 1dc874a51ad261073b51893980ba5e0737c63ab2 Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Tue, 25 Jun 2019 18:50:18 +0200 Subject: [PATCH 2/7] setup dockerfile --- geojson2csv/Dockerfile | 27 +++++++++++++++++++++++++++ geojson2csv/requirements.txt | 2 ++ 2 files changed, 29 insertions(+) create mode 100644 geojson2csv/Dockerfile create mode 100644 geojson2csv/requirements.txt diff --git a/geojson2csv/Dockerfile b/geojson2csv/Dockerfile new file mode 100644 index 0000000..c729a02 --- /dev/null +++ b/geojson2csv/Dockerfile @@ -0,0 +1,27 @@ +FROM python:3.7-alpine + +LABEL "name"="geojson2csv" +LABEL "maintainer"="Unacast " +LABEL "version"="1.0.0" + +LABEL "com.github.actions.name"="Geojson2csv" +LABEL "com.github.actions.description"="Convert geojson to csv" +LABEL "com.github.actions.icon"="truck" +LABEL "com.github.actions.color"="green" + +RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories + +RUN apk add --no-cache \ +gcc \ +libc-dev \ +geos-dev + +COPY requirements.txt / +RUN pip install -r /requirements.txt + +#ENV GITHUB_WORKSPACE /github/workspace +#WORKDIR /github/workspace + +COPY geojson2csv.py ${GITHUB_WORKSPACE}/ + +ENTRYPOINT [ "python", "geojson2csv.py"] \ No newline at end of file diff --git a/geojson2csv/requirements.txt b/geojson2csv/requirements.txt new file mode 100644 index 0000000..9fb3194 --- /dev/null +++ b/geojson2csv/requirements.txt @@ -0,0 +1,2 @@ +geojson +shapely From eb16fb15885982cefd83122655408390e74b84bb Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Tue, 25 Jun 2019 21:19:04 +0200 Subject: [PATCH 3/7] change script location --- geojson2csv/Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geojson2csv/Dockerfile b/geojson2csv/Dockerfile index c729a02..74898aa 100644 --- a/geojson2csv/Dockerfile +++ b/geojson2csv/Dockerfile @@ -19,9 +19,9 @@ geos-dev COPY requirements.txt / RUN pip install -r /requirements.txt -#ENV GITHUB_WORKSPACE /github/workspace -#WORKDIR /github/workspace +#ENV HOME /github/home +#WORKDIR /github/home -COPY geojson2csv.py ${GITHUB_WORKSPACE}/ +COPY geojson2csv.py "${HOME}"/. ENTRYPOINT [ "python", "geojson2csv.py"] \ No newline at end of file From e08aec01a61102b636fd8333166dfc34eefbca95 Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Wed, 26 Jun 2019 12:14:14 +0200 Subject: [PATCH 4/7] clean up dockerfile --- geojson2csv/Dockerfile | 8 +++----- geojson2csv/geojson2csv.py | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/geojson2csv/Dockerfile b/geojson2csv/Dockerfile index 74898aa..26fb58c 100644 --- a/geojson2csv/Dockerfile +++ b/geojson2csv/Dockerfile @@ -19,9 +19,7 @@ geos-dev COPY requirements.txt / RUN pip install -r /requirements.txt -#ENV HOME /github/home -#WORKDIR /github/home +COPY geojson2csv.py /usr/bin/geojson2csv +RUN chmod +x /usr/bin/geojson2csv -COPY geojson2csv.py "${HOME}"/. - -ENTRYPOINT [ "python", "geojson2csv.py"] \ No newline at end of file +CMD ["geojson2csv", "-h"] \ No newline at end of file diff --git a/geojson2csv/geojson2csv.py b/geojson2csv/geojson2csv.py index ddb72d4..2782329 100644 --- a/geojson2csv/geojson2csv.py +++ b/geojson2csv/geojson2csv.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python import argparse import csv import json From acb496b8dc5a961f90778d93b44418600ebcc28b Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Wed, 26 Jun 2019 12:37:02 +0200 Subject: [PATCH 5/7] added curl requirement for github api --- geojson2csv/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/geojson2csv/Dockerfile b/geojson2csv/Dockerfile index 26fb58c..3dc252c 100644 --- a/geojson2csv/Dockerfile +++ b/geojson2csv/Dockerfile @@ -14,7 +14,8 @@ RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/reposit RUN apk add --no-cache \ gcc \ libc-dev \ -geos-dev +geos-dev \ +curl COPY requirements.txt / RUN pip install -r /requirements.txt From 7b8efe99c87877ca20236bcdfb94cf6fcf7b9c81 Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Mon, 1 Jul 2019 11:26:17 +0200 Subject: [PATCH 6/7] Fix PR issues --- geojson2csv/Dockerfile | 3 +-- geojson2csv/requirements.txt | 2 -- geojson2csv/samples.geojson | 10 ---------- 3 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 geojson2csv/requirements.txt delete mode 100644 geojson2csv/samples.geojson diff --git a/geojson2csv/Dockerfile b/geojson2csv/Dockerfile index 3dc252c..d0fa312 100644 --- a/geojson2csv/Dockerfile +++ b/geojson2csv/Dockerfile @@ -17,8 +17,7 @@ libc-dev \ geos-dev \ curl -COPY requirements.txt / -RUN pip install -r /requirements.txt +RUN pip install geojson shapely COPY geojson2csv.py /usr/bin/geojson2csv RUN chmod +x /usr/bin/geojson2csv diff --git a/geojson2csv/requirements.txt b/geojson2csv/requirements.txt deleted file mode 100644 index 9fb3194..0000000 --- a/geojson2csv/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -geojson -shapely diff --git a/geojson2csv/samples.geojson b/geojson2csv/samples.geojson deleted file mode 100644 index 34b06e1..0000000 --- a/geojson2csv/samples.geojson +++ /dev/null @@ -1,10 +0,0 @@ -{ -"type": "FeatureCollection", -"name": "samples", -"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, -"features": [ -{ "type": "Feature", "properties": { "Feature 1": 1, "Feature 2": "AAA" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.291400058607934, 37.749843712357368 ], [ -122.341812276949355, 37.737194828482615 ], [ -122.324947098449684, 37.67926660667937 ], [ -122.252536821195633, 37.680733143940216 ], [ -122.291400058607934, 37.749843712357368 ] ] ] } }, -{ "type": "Feature", "properties": { "Feature 1": 2, "Feature 2": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.605788983900837, 37.747827223623695 ], [ -122.65858432529113, 37.683299584146667 ], [ -122.519996554141599, 37.656168644821101 ], [ -122.513397136467816, 37.725829164711072 ], [ -122.605788983900837, 37.747827223623695 ] ] ] } }, -{ "type": "Feature", "properties": { "Feature 1": 3, "Feature 2": "CCCCC" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.459135257816683, 38.084397524986819 ], [ -122.584524193618634, 38.028669109074848 ], [ -122.466467944120893, 37.96414146959782 ], [ -122.350611500514404, 38.05066716798747 ], [ -122.35281130640567, 38.103462509377763 ], [ -122.459135257816683, 38.084397524986819 ] ] ] } } -] -} From ba06537e8895fbb50bdf66c2ae52de42966bedbf Mon Sep 17 00:00:00 2001 From: Stefan Mandaric Date: Mon, 1 Jul 2019 12:37:47 +0200 Subject: [PATCH 7/7] Add readme --- geojson2csv/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 geojson2csv/README.md diff --git a/geojson2csv/README.md b/geojson2csv/README.md new file mode 100644 index 0000000..e28c509 --- /dev/null +++ b/geojson2csv/README.md @@ -0,0 +1,21 @@ +# geojson2csv +Github Action for tansforming geojson FeatureCollections to CSVs with WKT geometry. + +## Usage + +## Example Workflow file + +An example workflow to transform a geojson in the repository + +``` +workflow "Deploy geometries" { + on = "deployment" + resolves = "Transform to csv" +} + +action "Transform to csv" { + uses = "unacast/actions/geojson2csv@master" + runs = "geojson2csv" + args = "input.geojson output.csv" +} +``` \ No newline at end of file