jsongoogle-mapsgeolocationgissimplegeo

Can I load a .csv into Simplegeo Storage?


Is there a straightforward way to load a .csv file into Simplegeo Storage? I don't have great coding skills and I'm trying to get things set up so I can ask a freelancer to create some maps for my app. If someone has existing code to do this I can probably figure out how to make it work for my situation.


Solution

  • I just skimmed over the api. Here's a basic example in python

    Assumed csv format:

    layer, id, lat, lon
    

    python

    from simplegeo.models import Record, Client
    
    lines = open('file.csv').split('\n')
    client = Client('your-oauth-token', 'your-oauth-secret')
    
    for line in lines:
        parts = line.split(',')
        if len(parts) == 4:
            layer = parts[0].strip()
            id = parts[1].strip()
            lat = float(parts[2].strip())
            lon = float(parts[3].strip())
            r = Record(layer, id, lat, lon)
            client.storage.add_record(r)
    

    After a bit more digging, I found a python example on their site for this exact purpose https://simplegeo.com/docs/tutorials/general-hackery#how-import-csv-file-simplegeo

    import csv
    import simplegeo
    
    OAUTH_TOKEN = '[insert_oauth_token_here]'
    OAUTH_SECRET = '[insert_oauth_secret_here]'
    CSV_FILE = '[insert_csv_file_here]'
    LAYER = '[insert_layer_name_here]'
    
    client = simplegeo.Client(OAUTH_TOKEN, OAUTH_SECRET)
    
    def insert(data):
        layer = LAYER
        id=data.pop("id")
        lat=data.pop("latitude")
        lon=data.pop("longitude")
        # Grab more columns if you wish
        record = simplegeo.Record(layer,id,lat,lon,**data)
        client.add_record(record)
    
    r = csv.DictReader(open(CSV_FILE, mode='U'))
    for l in r:
        insert(l)