Skip to content

Instantly share code, notes, and snippets.

@akent
Created September 29, 2017 01:33
Show Gist options
  • Save akent/4d0699eeda6afbdbfcc57cc0554ef37a to your computer and use it in GitHub Desktop.
Save akent/4d0699eeda6afbdbfcc57cc0554ef37a to your computer and use it in GitHub Desktop.
get UGC and FIPS from lat/lng
package main
import (
"fmt"
"log"
"github.com/jonas-p/go-shp"
geo "github.com/kellydunn/golang-geo"
)
func main() {
u, d := getUGC(-104.8547, 38.9008)
f := getFIPS(-104.8547, 38.9008)
fmt.Println(u, d)
fmt.Println(f)
}
func getUGC(lat, lng float64) (string, string) {
shape, err := shp.Open("z_04ap17.shp")
if err != nil {
log.Fatal(err)
}
defer shape.Close()
fields := shape.Fields()
desiredPoint := geo.NewPoint(lat, lng)
for shape.Next() {
n, p := shape.Shape()
poly, ok := p.(*shp.Polygon)
if !ok {
fmt.Println("Failed to type assert")
}
var pointArr = make([]*geo.Point, len(poly.Points))
for i, point := range poly.Points {
pointArr[i] = geo.NewPoint(point.X, point.Y)
}
geop := geo.NewPolygon(pointArr)
if geop.Contains(desiredPoint) {
var ugc, desc string
for k, f := range fields {
if f.String() == "STATE_ZONE" {
ugc = shape.ReadAttribute(n, k)
}
if f.String() == "NAME" {
desc = shape.ReadAttribute(n, k)
}
}
return ugc, desc
}
}
fmt.Println("didn't find it")
return "", ""
}
func getFIPS(lat, lng float64) string {
shape, err := shp.Open("c_11au16.shp")
if err != nil {
log.Fatal(err)
}
defer shape.Close()
fields := shape.Fields()
desiredPoint := geo.NewPoint(lat, lng)
for shape.Next() {
n, p := shape.Shape()
poly, ok := p.(*shp.Polygon)
if !ok {
fmt.Println("Failed to type assert")
}
var pointArr = make([]*geo.Point, len(poly.Points))
for i, point := range poly.Points {
pointArr[i] = geo.NewPoint(point.X, point.Y)
}
geop := geo.NewPolygon(pointArr)
if geop.Contains(desiredPoint) {
var fips string
for k, f := range fields {
if f.String() == "FIPS" {
fips = shape.ReadAttribute(n, k)
}
}
return fips
}
}
fmt.Println("didn't find it")
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment