Last active
August 30, 2018 16:06
-
-
Save s-l-teichmann/7894520f3038584c92a98fafff447d18 to your computer and use it in GitHub Desktop.
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 | |
import ( | |
"database/sql" | |
"time" | |
) | |
const querySQL = ` | |
SELECT | |
st_x(koordinate), | |
st_y(koordinate), | |
zeit, | |
strom, | |
round((st_distance_sphere(koordinate,ST_GeomFromText('POINT(8.3726 48.9992)', 4326))/1000)::numeric,1) as dist, | |
typ | |
FROM blitz.blitze | |
where zeit between '2018-07-17 21:00' and '2018-07-17 22:00' | |
and round((st_distance_sphere(koordinate,ST_GeomFromText('POINT(8.3726 48.9992)', 4326))/1000)::numeric,1) < 10 | |
order by dist asc | |
` | |
type Feature struct { | |
Type string `json:"type"` | |
Geometry *map[string]interface{} `json:"geometry"` | |
Properties *map[string]interface{} `json:"properties,omitempty"` | |
} | |
func getFeatures(db *sql.DB) ([]*Feature, error) { | |
rows, err := db.Query(querySQL) | |
if err != nil { | |
return nil, err | |
} | |
defer rows.Close() | |
var features []*Feature | |
for rows.Next() { | |
var ( | |
coords [2]float64 | |
zeit time.Time | |
strom float64 | |
dist float64 | |
typ int | |
) | |
if err := rows.Scan( | |
&coords[0], &coords[1], | |
&zeit, | |
&strom, | |
&dist, | |
&typ, | |
); err != nil { | |
return nil, err | |
} | |
feature := &Feature{ | |
Type: "feature", | |
Geometry: &map[string]interface{}{ | |
"type": "Point", | |
"coordinates": coords[:], | |
}, | |
Properties: &map[string]interface{}{ | |
"zeit": zeit.String(), | |
"strom": strom, | |
"dist": dist, | |
"typ": typ, | |
}, | |
} | |
features = append(features, feature) | |
} | |
return features, rows.Err() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment