mirror of
https://github.com/zhigang1992/actions.git
synced 2026-01-12 22:45:39 +08:00
Merge pull request #18 from unacast/geojson2csv
Add an action for geojson2csv
This commit is contained in:
25
geojson2csv/Dockerfile
Normal file
25
geojson2csv/Dockerfile
Normal file
@@ -0,0 +1,25 @@
|
||||
FROM python:3.7-alpine
|
||||
|
||||
LABEL "name"="geojson2csv"
|
||||
LABEL "maintainer"="Unacast <developers+github@unacast.com>"
|
||||
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 \
|
||||
curl
|
||||
|
||||
RUN pip install geojson shapely
|
||||
|
||||
COPY geojson2csv.py /usr/bin/geojson2csv
|
||||
RUN chmod +x /usr/bin/geojson2csv
|
||||
|
||||
CMD ["geojson2csv", "-h"]
|
||||
21
geojson2csv/README.md
Normal file
21
geojson2csv/README.md
Normal file
@@ -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"
|
||||
}
|
||||
```
|
||||
35
geojson2csv/geojson2csv.py
Normal file
35
geojson2csv/geojson2csv.py
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python
|
||||
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)
|
||||
Reference in New Issue
Block a user