Saturday, June 28, 2014

I'm very ghetto

I shat out a quick connectwise to KML python script so sales ppl can look at their turf in google earth and etc.  I am a bad person.  Bad in every way.  This will be refined into something I'm not embarrassed of, but I was surprised something like this wasn't on the internet already.

import pyodbc
from geopy.geocoders import Bing
import simplekml
bingkey="lsdfkglksjdfglksfdhlkjghsdlkfjhglskdfjhgsjdgjsjdf"
geolocator = Bing(bingkey)
kml = simplekml.Kml()
conn = pyodbc.connect('Driver=FreeTDS;Server=10.x.x.x;port=1433;uid=sqlreporting;pwd=blabalbalbalablab;database=blerblebleblrblebr')
cur = conn.cursor()
q="""
SELECT Company.Company_Name,Address_Line1,City,State_ID,ZIP
from Company INNER JOIN Company_Address on Company.Company_RecID=Company_Address.Company_RecID
and Company.Company_Status_RecID=1
"""
cur.execute(q)
i=1
for row in cur.fetchall():
        address=''
        company = row[0]
        addressline1 = row[1]
        addresscity = row[2]
        addressstate = row[3]
        addresszip = row[4]
        if (isinstance(addressline1, str) and isinstance(addresscity,str) and isinstance(addressstate,str)):
                address = addressline1 + ' ' + addresscity + ' ' + addressstate
        fulladdress=''
        latitude=''
        longitude=''
        try:
                fulladdress, (latitude, longitude) = geolocator.geocode(address)
        except:
                print("found a bad address: " + company + ' ' + address)
        if (fulladdress != ''):
                print (i, company)
                kml.newpoint(name=company, coords=[(longitude,latitude)])
        i=i+1
kml.save("bigtest.kml")
cur.close()
conn.close()

No comments:

Post a Comment