示例#1
0
文件: csv.go 项目: buckhx/diglet
func (c *CsvSource) publishLines() (lines chan []string, err error) {
	//TODO optionally trim lines
	f, err := os.Open(c.path)
	if err != nil {
		return
	}
	reader := csv.NewReader(f)
	c.headers = readHeaders(reader, c.filter)
	//TODO if err != nil
	lines = make(chan []string, 100)
	go func() {
		defer close(lines)
		defer f.Close()
		for {
			line, err := reader.Read()
			if err == io.EOF {
				break
			}
			if err != nil {
				util.Warn(err, "line reading")
			} else if line[c.headers[c.fields["lat"]]] == "" || line[c.headers[c.fields["geometry"]]] == "" {
				continue
				//err = util.Errorf("No coordinates %v", line)
				//util.Warn(err, "no lat/lon")
			} else {
				lines <- line
			}
		}
	}()
	return
}
示例#2
0
文件: csv.go 项目: buckhx/diglet
func (c *CsvSource) Publish() (features chan *geo.Feature, err error) {
	lines, err := c.publishLines()
	if err != nil {
		return
	}
	features = make(chan *geo.Feature)
	go func() {
		defer close(features)
		//TODO read ID from csv
		id := uint64(0)
		for line := range lines {
			if feature, err := c.featureAdapter(line); err != nil {
				util.Warn(err, "feature adapter")
			} else {
				if feature.ID != nil {
					feature.Properties["fid"] = feature.ID
				}
				feature.ID = id
				features <- feature
				id++
			}
		}
	}()
	return
}
示例#3
0
文件: layer.go 项目: buckhx/diglet
func getVtValue(val interface{}) (vt_val *vt.Tile_Value) {
	vt_val = &vt.Tile_Value{}
	switch v := val.(type) {
	case string:
		vt_val.StringValue = &v
	case float32:
		vt_val.FloatValue = &v
	case float64:
		vt_val.DoubleValue = &v
	case int:
		intv := int64(v)
		vt_val.IntValue = &intv
	case int32:
		intv := int64(v)
		vt_val.IntValue = &intv
	case int64:
		vt_val.IntValue = &v
	case uint:
		uintv := uint64(v)
		vt_val.UintValue = &uintv
	case uint32:
		uintv := uint64(v)
		vt_val.UintValue = &uintv
	case uint64:
		vt_val.UintValue = &v
	case bool:
		vt_val.BoolValue = &v
	case nil:
		// nothing
	default:
		//TODO, flatten maps
		err := util.Errorf("Bad interface{} for vt.Tile_Value  %v", val)
		util.Warn(err, "attempting to cast value to json string")
		if b, err := json.Marshal(v); err == nil {
			s := string(b)
			vt_val.StringValue = &s
		} else {
			util.Warn(err, "json cast failed, skipping...")
		}
	}
	return
}
示例#4
0
文件: geojson.go 项目: buckhx/diglet
func publishFeatureCollection(collection *geojson.FeatureCollection) (features chan *Feature) {
	features = make(chan *Feature, 10)
	go func() {
		defer close(features)
		for _, feature := range collection.Features {
			f, err := GeojsonFeatureAdapter(feature)
			util.Warn(err, "feature publishing")
			features <- f
		}
	}()
	return
}
示例#5
0
文件: csv.go 项目: buckhx/diglet
func readHeaders(reader *csv.Reader, filter mapset.Set) (headers map[string]int) {
	line, err := reader.Read()
	util.Warn(err, "reading headers")
	headers = make(map[string]int, len(line))
	for i, k := range line {
		//if _, ok := c.fields[k]; !ok {
		k = util.Slugged(k, "_")
		if filter == nil || filter.Contains(k) {
			headers[k] = i
		}
		//}
	}
	util.Debug("Headers %v", headers)
	return
}