示例#1
0
// PathToGeoJSON ..
func (t Tracker) PathToGeoJSON(path []geo.Point) (string, error) {
	fc := new(gj.FeatureCollection)
	// Start marker
	var p *gj.Feature
	start := new(gj.Point)
	start.Type = `Point`
	c := new(gj.Coordinate)
	c[0] = gj.Coord(path[0].Lng)
	c[1] = gj.Coord(path[0].Lat)
	start.Coordinates = *c

	// Path line
	var f *gj.Feature
	line := new(gj.LineString)
	line.Type = `LineString`
	//var cordinates gj.Coordinates

	for _, p := range path {
		coord := new(gj.Coordinate)
		coord[0] = gj.Coord(p.Lng)
		coord[1] = gj.Coord(p.Lat)
		line.AddCoordinates(*coord)
		//cordinates = append(cordinates, *coord)
	}
	//line.AddCoordinates(cordinates)

	fmt.Println(line.Type)
	f = gj.NewFeature(line, nil, nil)
	p = gj.NewFeature(start, nil, nil)
	fc.AddFeatures(f, p)

	fc.Type = `FeatureCollection`
	geojson, e := gj.Marshal(fc)
	return geojson, e
}
示例#2
0
func makeFeaturefromRecord(r *Record) string {
	var f *gj.Feature
	lon := gj.Coord(r.Longitude)
	lat := gj.Coord(r.Latitude)
	p := gj.NewPoint(gj.Coordinate{lon, lat})
	id := r.BusID
	props := make(map[string]interface{})
	props["BusName"] = r.BusName
	props["RouteID"] = r.RouteID
	props["TripID"] = r.TripID
	props["TripHeadsign"] = r.TripHeadsign
	props["Service"] = r.Service
	props["LocationUpdated"] = r.LocationUpdated
	f = gj.NewFeature(p, props, id)
	gjstr, err := gj.Marshal(f)
	if err != nil {
		log.Fatal("geojson marshall failed", err)
	}
	return gjstr
}
示例#3
0
// isOtherGeo searches for non-standard geo data in the given json map. It looks for the presence
// of lat/lng (and a few variations thereof) or x/y values in the object as well as a distance/radius/accuracy
// field and creates a geojson point out of it and returns that, along with a boolean value
// representing whether or not it found any geo data in the object. It will also look for
// any keys that hold an array of two numbers with a key name that suggests that it might
// be a lng/lat array.
//
// The following keys will be detected as Latitude:
//	"lat", "latitude"
//	"y"
//
// The following keys will be detected as Longitude:
//	"lng", "lon", "long", "longitude"
//	"x"
//
// The following keys will be used to fill the "radius" property of the resulting geojson:
//	"dist", "distance"
//	"rad", "radius"
//	"acc", "accuracy"
//
// The following keys will be searched for a long/lat pair:
//	"geo"
//	"loc" or "location"
//	"coord", "coords", "coordinate" or "coordinates"
func isOtherGeo(o map[string]interface{}) (bool, *Geo) {
	var foundLat, foundLng, foundDst bool
	var lat, lng, dst float64

	for k, v := range o {
		switch strings.ToLower(k) {
		case "lat", "latitude", "y":
			lat, foundLat = v.(float64)
		case "lng", "lon", "long", "longitude", "x":
			lng, foundLng = v.(float64)
		case "dst", "dist", "distance", "rad", "radius", "acc", "accuracy":
			dst, foundDst = v.(float64)
		case "geo", "loc", "location", "coord", "coordinate", "coords", "coordinates":
			g, ok := v.([]float64)
			if !ok || len(g) != 2 {
				break
			}

			lng, lat = g[0], g[1]
			foundLat, foundLng = true, true
		}
	}

	if foundLat && foundLng && latIsValid(lat) && lngIsValid(lng) {
		p := gj.NewPoint(gj.Coordinate{gj.CoordType(lng), gj.CoordType(lat)})
		pstr, _ := gj.Marshal(p)
		var geo map[string]interface{}
		json.Unmarshal([]byte(pstr), &geo)
		debugLog("Found other geo:", geo)
		g := &Geo{
			Geo: geo,
		}
		if foundDst {
			g.Radius = dst
		}
		return true, g
	}

	return false, nil
}