Skip to content
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

Add convertion type geojson #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ You can then install Shapely using this command:

python -m pip install Shapely-X-cpX-cpXm-winX.whl

#### `-f geojson`

Using the geojson convertion type requires the geojson library which can be installed with

pip install geojson

### Available formats

Expand Down Expand Up @@ -108,8 +113,11 @@ that can then be handled without iterative mode (necessary for gpxtracks and the

#### gpx
GPS Exchange Format including location, timestamp, and accuracy/speed/altitude as available.
Data produced is valid GPX 1.1. Points are stored as individual, unrelated waypoints (like the other formats, except for gpxtracks).
Data produced is valid GPX 1.1. Points are stored as individual, unrelated waypoints (like the other formats, except for gpxtracks).

#### gpxtracks
GPS Exchange Format including location, timestamp, and accuracy/speed/altitude as available.
Data produced is valid GPX 1.1. Points are grouped together into tracks by time and location (specifically, two chronological points split a track if they differ by over 10 minutes or approximately 40 kilometers).
Data produced is valid GPX 1.1. Points are grouped together into tracks by time and location (specifically, two chronological points split a track if they differ by over 10 minutes or approximately 40 kilometers).

#### geojson
Produces a JSON file with a geojson line string containing the points.
21 changes: 19 additions & 2 deletions location_history_json_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
else:
shapely_available = True

try:
import geojson
except ImportError:
geojson_available = False
else:
geojson_available = True
geojson_data = []

def _valid_date(s):
try:
Expand Down Expand Up @@ -308,6 +315,9 @@ def _write_location(output, format, location, separator, first, last_location):
output.write(" </desc>\n")
output.write(" </trkpt>\n")

if format == "geojson":
geojson_data.append((location["longitudeE7"] / 10000000, location["latitudeE7"] / 10000000, math.floor(int(location['timestampMs'])/1000)))


def _write_footer(output, format):
"""Writes the file footer for the specified format to output"""
Expand All @@ -329,6 +339,9 @@ def _write_footer(output, format):
output.write("</gpx>\n")
return

if format == "geojson":
output.write(geojson.dumps(geojson.LineString(geojson_data)))


def convert(locations, output, format="kml",
js_variable="locationJsonData", separator=",",
Expand All @@ -347,7 +360,7 @@ def convert(locations, output, format="kml",

format: str
Format to convert to
Can be one of "kml", "json", "js", "jsonfull", "jsfull", "csv", "csvfull", "csvfullest", "gpx", "gpxtracks"
Can be one of "kml", "json", "js", "jsonfull", "jsfull", "csv", "csvfull", "csvfullest", "gpx", "gpxtracks", "geojson"
See README.md for details about those formats

js_variable: str
Expand Down Expand Up @@ -430,7 +443,7 @@ def main():
arg_parser.add_argument(
"-f",
"--format",
choices=["kml", "json", "js", "jsonfull", "jsfull", "csv", "csvfull", "csvfullest", "gpx", "gpxtracks"],
choices=["kml", "json", "js", "jsonfull", "jsfull", "csv", "csvfull", "csvfullest", "gpx", "gpxtracks", "geojson"],
default="kml",
help="Format of the output"
)
Expand Down Expand Up @@ -505,6 +518,10 @@ def main():

polygon = Polygon(ext)

if not geojson_available and args.format == "geojson":
print("geojson is not available. Please install with `pip install geojson` and try again.")
return

if args.iterative:
if args.chronological:
print("-----------------------------------")
Expand Down