Skip to content

Commit

Permalink
Merge pull request #4 from Kasluk24/overwrite
Browse files Browse the repository at this point in the history
Switch to overwrite files even if no export dir is specified
  • Loading branch information
Kasluk24 authored Jan 26, 2021
2 parents e8a7d83 + bc283d2 commit 08cc188
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Python command line application to convert BrokJSON files to GeoJSON files and v
What is [BrokJSON](https://www.brokjson.dev/)?

## Installation
Minimal required Python version: 3.4

### Get directly from GitHub and install with pip
- Execute the following command in the command line <br>
`pip install git+https://github.com/Kasluk24/brokjson-file-converter.git#egg=brokjson-file-converter`
Expand Down Expand Up @@ -32,11 +34,17 @@ geo2brok = from GeoJSON to BrokJSON, brok2geo = BrokJSON to GeoJSON
- To convert all files in a directory with the ending "\*.json", just specifie the directory instead of the filename
- If no export directory is specified, the files are written to a subfolder named "geojson" or "brokjson" depending on the output format

### Overwrite / keep files
- By default, the converter creates a new folder if no export directory or file has been specified and keeps the original files
- With the flag "-o" or "--overwrite" the original files are overwritten, even if no export directory / file is specified
- If an export directory or file has been specified, existing files in the directory are overwritten if they have the same name

## Examples
- Convert the GeoJSON file "GeoJSON.json" to the BrokJSON file BrokJSON.json<br>
`python -m brokjson-file-converter geo2brok GeoJSON.json BrokJSON.json`
- Convert the brokJSON file "BrokJSON.json" to the GeoJSON file GeoJSON.json<br>
`python -m brokjson-file-converter brok2geo BrokJSON.json GeoJSON.json`
- Convert all "\*.json" files inside the directory "brokfiles" to GeoJSON files into the directory "geofiles"
- Convert all "\*.json" files inside the directory "brokfiles" to GeoJSON files into the directory "geofiles"<br>
`python -m brokjson-file-converter brok2geo brokfiles geofiles`

- Convert all "\*.json" files inside the directory "geofiles" to BrokJSON files and overwrite the original files<br>
`python -m brokjson-file-converter -o geo2brok geofiles`
22 changes: 18 additions & 4 deletions brokjson-file-converter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
def toBrok(infile, outfile=None):
# Specifie outfile if not given and create directory
if not outfile:
outfile = Path(infile.parents[0] / 'brokjson' / infile.name)
if args.overwrite == 1:
outfile = infile
else:
outfile = Path(infile.parents[0] / 'brokjson' / infile.name)

outfile.parents[0].mkdir(exist_ok=True)

Expand All @@ -25,7 +28,10 @@ def toBrok(infile, outfile=None):
def toGeo(infile, outfile=None):
# Specifie outfile if not given and create directory
if not outfile:
outfile = Path(infile.parents[0] / 'geojson' / infile.name)
if args.overwrite == 1:
outfile = infile
else:
outfile = Path(infile.parents[0] / 'geojson' / infile.name)

outfile.parents[0].mkdir(exist_ok=True)

Expand All @@ -42,7 +48,10 @@ def toGeo(infile, outfile=None):
def toBrokMass(indir, outdir=None):
# Specifie output directory if not given and create it
if not outdir:
outdir = Path(indir / 'brokjson')
if args.overwrite == 1:
outdir = indir
else:
outdir = Path(indir / 'brokjson')

outdir.mkdir(parents=True, exist_ok=True)

Expand All @@ -62,7 +71,11 @@ def toBrokMass(indir, outdir=None):
def toGeoMass(indir, outdir=None):
# Specifie output directory if not given and create it
if not outdir:
outdir = Path(indir / 'geojson')
if args.overwrite == 1:
outdir = indir
else:
outdir = Path(indir / 'geojson')


outdir.mkdir(parents=True, exist_ok=True)

Expand All @@ -82,6 +95,7 @@ def toGeoMass(indir, outdir=None):
# Argument parser
parser = argparse.ArgumentParser(description='Convert GeoJSON files to BrokJSON files and vice versa from command line')
parser.add_argument('function', choices=['geo2brok', 'brok2geo'], help='Direction of conversion: geo2brok = GeoJSON to BrokJSON, brok2geo = BrokJSON to GeoJSON')
parser.add_argument('-o', '--overwrite', dest='overwrite', action='store_const', const=1, default=0, help='Overwrite already existing files, even if no export directory is specified')
parser.add_argument('inpath', metavar='I', type=str, nargs=1, help='GeoJSON or BrokJSON file or directory to convert')
parser.add_argument('outpath', metavar='O', type=str, nargs='*', help='Converted file or output directory')
args = parser.parse_args()
Expand Down
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import pathlib
import os
from setuptools import setup

# The directory containing this file
HERE = pathlib.Path(__file__).parent
HERE = os.path.dirname(__file__)

# The text of the README file
README = (HERE / "README.md").read_text()
README = open(os.path.join(HERE, "README.md"), 'r').read()

# This call to setup() does all the work
setup(
name="brokjson-file-converter",
version="0.0.1",
version="1.0.0",
description="Convert GeoJSON files to BrokJSON files and vice versa from command line",
long_description=README,
long_description_content_type="text/markdown",
Expand All @@ -24,5 +24,6 @@
],
packages=["brokjson-file-converter"],
include_package_data=True,
python_requires='>=3.4',
install_requires=['brokjson']
)

0 comments on commit 08cc188

Please sign in to comment.