Create a gist now

Instantly share code, notes, and snippets.

ingressjson2kml.py
# -*- coding: utf-8 -*-
import sys
import json
import simplekml
class Portal:
def __init__(self, lat, lon, portal_name, portal_photo_url):
self.lat = lat # latitude
self.lon = lon # longitude
self.portal_name = portal_name # Portal Name
# Photo URL
self.portal_photo_url = portal_photo_url
def get_map_child(map_array):
result = []
for map_data in map_array:
result.append(map_data)
return result
def get_portal_arr(map_name_arr, obj):
result = []
for map_name in map_name_arr:
for entity in obj['result']['map'][map_name]['gameEntities']:
# get portal entities
if entity[2][0] == 'p':
# latitude
lat = float(entity[2][2] / 1000000)
# longitude
lon = float(entity[2][3] / 1000000)
# Portalk Name
portal_name = entity[2][8]
# Photo URL
portal_photo_url = entity[2][7]
# Once stored in an array
result.append(
Portal(lat, lon, portal_name, portal_photo_url))
return result
def output_kml_file(sorted_portal_arr, output_kml_file_name):
kml = simplekml.Kml()
for portal in sorted_portal_arr:
# setting photo information for google mymap
extendeddata = simplekml.ExtendedData()
extendeddata.newdata('gx_media_links', portal.portal_photo_url)
kml.newpoint(
name=portal.portal_name,
coords=[(portal.lon, portal.lat)],
extendeddata=extendeddata)
# output kml
kml.save(output_kml_file_name)
def main():
args = sys.argv
if args[1] is None:
print('error!')
exit
input_json_file_name = args[1] + '.json' # input file name
output_kml_file_name = args[1] + '.kml' # output file name
with open(input_json_file_name, 'r') as f:
for line in f:
# read file(maybe 1 line)
obj = json.loads(line)
# get child entity name
map_name_arr = get_map_child(obj['result']['map'])
# get portal array
portal_arr = get_portal_arr(map_name_arr, obj)
# sort(latitude descending)
sorted_portal_arr = sorted(
portal_arr, key=lambda h: h.lat, reverse=True)
# output
output_kml_file(sorted_portal_arr, output_kml_file_name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment