Created
February 10, 2015 22:13
-
-
Save gleicon/2bd294a26cfffb7393c4 to your computer and use it in GitHub Desktop.
geocoding stuff with google maps visualization python + go, quick and dirt. servemaps serves the /map url which renders a simple map with markers read from a csv file. geocode.py helps if you happen to have a lfile with a list of addresses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert a file from name;address to name address,latitude,longitude | |
# pip install geopy | |
from geopy import geocoders | |
geolocator = geocoders.GoogleV3() | |
import sys | |
def geoconvert(file): | |
f = open(file) | |
for l in f.readlines(): | |
l = l.strip() | |
name, addr = l.split(';') | |
location = geolocator.geocode(addr) | |
if location is not None: | |
print "%s %s,%s,%s" % (name, addr, location.latitude, location.longitude) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print "usage: python geocode.py <file>" | |
sys.exit(-1) | |
geoconvert(sys.argv[1]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> | |
<meta charset="utf-8"> | |
<title>map</title> | |
<style> | |
html, body, #map-canvas { | |
height: 100%; | |
margin: 0px; | |
padding: 0px | |
} | |
</style> | |
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> | |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script> | |
<script> | |
$(function () { | |
function initialize() { | |
var myLatlng = new google.maps.LatLng(-25.363882,-46.683585); | |
var mapOptions = { | |
zoom: 4, | |
center: myLatlng | |
} | |
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); | |
$.ajax({ | |
type: 'GET', | |
async : true, | |
global: 'false', | |
url: '/api/v1/points', | |
headers : {Accept: 'application/json'}, | |
dataType: 'json' | |
}).done(function(points) { | |
$.each(points, function(index, point){ | |
if(point) { | |
new google.maps.Marker({ | |
position: new google.maps.LatLng(point.Lat, point.Long), | |
map: map, | |
title: "name"+ point.Name | |
}); | |
} | |
}) | |
}); | |
} | |
//google.maps.event.addDomListener(window, 'load', initialize); | |
initialize() | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="map-canvas"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
Serves a map with markers read from a csv file following the format: | |
name,latitude,longitude | |
$ servemaps.go -f file.csv -p 9090 | |
*/ | |
import ( | |
"encoding/csv" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
) | |
type PointRecord struct { | |
Name string | |
Lat string | |
Long string | |
} | |
func usage() { | |
fmt.Println("Usage: servemaps [-p port] -f file.csv") | |
fmt.Println("default port: 8080") | |
fmt.Println("file.csv is name,lat,long") | |
os.Exit(1) | |
} | |
func pointsToJSON(pp *[]PointRecord) (string, error) { | |
jsonStatus, err := json.Marshal(pp) | |
if err != nil { | |
return "", err | |
} | |
return string(jsonStatus), nil | |
} | |
func readCsv(filename string) (*[]PointRecord, error) { | |
csvfile, err := os.Open(filename) | |
if err != nil { | |
return nil, err | |
} | |
defer csvfile.Close() | |
reader := csv.NewReader(csvfile) | |
reader.FieldsPerRecord = -1 | |
rawCSVdata, err := reader.ReadAll() | |
if err != nil { | |
return nil, err | |
} | |
var Point PointRecord | |
var Points []PointRecord | |
for _, each := range rawCSVdata { | |
Point.Name = each[0] | |
Point.Lat = each[1] | |
Point.Long = each[2] | |
Points = append(Points, Point) | |
} | |
return &Points, nil | |
} | |
func main() { | |
port := flag.String("p", "8080", "Bind Port") | |
filename := flag.String("f", "", "csv file") | |
flag.Usage = usage | |
flag.Parse() | |
if *filename == "" { | |
usage() | |
} | |
pp, err := readCsv(*filename) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(-1) | |
} | |
http.HandleFunc("/api/v1/points", func(w http.ResponseWriter, r *http.Request) { | |
pj, err := pointsToJSON(pp) | |
if err != nil { | |
fmt.Println(err) | |
http.Error(w, "Internal Error", http.StatusInternalServerError) | |
return | |
} | |
fmt.Fprintf(w, pj) | |
}) | |
http.HandleFunc("/map", func(w http.ResponseWriter, r *http.Request) { | |
http.ServeFile(w, r, "map.html") | |
}) | |
address := fmt.Sprintf(":%s", *port) | |
fmt.Println(address) | |
panic(http.ListenAndServe(address, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment