示例#1
0
func importDatasource(importFile string) {
	fmt.Println("Importing", importFile)
	// setup database
	setupDb()
	// get geojson file
	var geojsonFile string
	ext := strings.Split(importFile, ".")[1]
	// convert shapefile
	if ext == "shp" {
		// Convert .shp to .geojson
		// ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp
		geojsonFile := strings.Replace(importFile, ".shp", ".geojson", -1)
		// fmt.Println("ogr2ogr", "-f", "GeoJSON", "-t_srs", "crs:84", geojsonFile, shapefile)
		out, err := exec.Command("ogr2ogr", "-f", "GeoJSON", "-t_srs", "crs:84", geojsonFile, importFile).Output()
		if err != nil {
			fmt.Println(err)
			fmt.Println(string(out))
			os.Exit(1)
		} else {
			fmt.Println(geojsonFile, "created")
			fmt.Println(string(out))
		}
	} else if ext == "geojson" {
		geojsonFile = importFile
	} else {
		fmt.Println("Unsupported file type", ext)
		os.Exit(1)
	}
	// Read .geojson file
	file, err := ioutil.ReadFile(geojsonFile)
	if err != nil {
		fmt.Printf("File error: %v\n", err)
		os.Exit(1)
	}
	// Unmarshal to geojson struct
	geojs, err := geojson.UnmarshalFeatureCollection(file)
	if err != nil {
		fmt.Printf("Unmarshal GeoJSON error: %v\n", err)
		os.Exit(1)
	}
	// Create datasource
	ds, _ := utils.NewUUID()
	app.DB.InsertLayer(ds, geojs)
	fmt.Println("Datasource created:", ds)
	// Cleanup artifacts
	if geojsonFile != importFile {
		os.Remove(geojsonFile)
	}
}
示例#2
0
// NewLayer creates new datasource layer
// @returns string - datasource id
// @returns Error
func (self *Database) NewLayer() (string, error) {
	// create geojson
	datasource, _ := utils.NewUUID()
	geojs := geojson.NewFeatureCollection()
	// convert to bytes
	value, err := geojs.MarshalJSON()
	if err != nil {
		return "", nil
	}
	self.commit_log_queue <- `{"method": "new_layer", "data": { "datasource": "` + datasource + `", "layer": ` + string(value) + `}}`
	// Insert layer into database
	err = self.Insert("layers", datasource, value)
	if err != nil {
		panic(err)
	}
	return datasource, err
}