I am looping through the amount of IP addresses in a traceroute and getting the geo-locational data from ip-api.com, I have then added the data that has been passed back into a variable called info.
def toKML(iplist,namelist):
kml = simplekml.Kml()
x = 0
for ip in iplist:
url =urllib2.Request('http://ip-api.com/json/' + str(ip)) #using API to gather geo info
info =json.load(urllib2.urlopen(url)) # setting data value to the feedback from the API for each IP
if 'city' in info:
print info['lon']
kml.newpoint(name=str(info['city']),coords=[str(info['lon']),str(info['lat'])],description=(namelist[x]))
x += 1
else:
print "Geo-locational data not found for the ip "+ ip +"."
x +=1
kml.save("test.kml")
An example of the JSON Object returned by the API is as follows:
{"as":"AS15169 Google Inc.","city":"Mountain View","country":"United States","countryCode":"US","isp":"Google","lat":37.386,"lon":-122.0838,"org":"Google","query":"8.8.8.8","region":"CA","regionName":"California","status":"success","timezone":"America/Los_Angeles","zip":"94040"}
This produces a KML document yet it isn't parsing the coordinates correctly, here is an exerpt of the KML document:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document id="feat_1">
<Placemark id="feat_2">
<name/>
<Point id="geom_0">
<coordinates>-,0,. 5,1,.</coordinates>
</Point>
<description>sgyl-core-2b-ae7-717.network.virginmedia.net</description>
</Placemark>
The coordinates are correct within the JSON object as when I print the 'lon' values within 'info'it returns:
-0.13 -0.0931 -0.0931 -0.13 -0.13 -0.13 -122.1826 -6.2597 -6.2597 -6.2597 -122.1822 -0.0931 -0.0931 -122.1822
The error resides within the code:
kml.newpoint(name=str(info['city']),coords=[str(info['lon']),str(info['lat'])],description=(namelist[x]))
Any help on the matter is greatly appreciated.