Example #1
0
// buildMagnacarto builds the mapnik XML or mapserver MAP (when mapfile is true) with magnacarto
func buildMagnacarto(t *testing.T, c testCase, mapfile bool) {
	caseBuildDir := filepath.Join("build", c.Name)

	suffix := ".xml"
	if mapfile {
		suffix = ".map"
	}

	conf := config.Magnacarto{BaseDir: caseBuildDir}
	here, _ := os.Getwd()
	conf.Mapnik.FontDirs = []string{here}
	locator := conf.Locator()

	var m builder.MapWriter
	if mapfile {
		m = mapserver.New(locator)
	} else {
		m = mapnik.New(locator)
	}

	b := builder.New(m)
	b.SetMML(filepath.Join(caseBuildDir, "test.mml"))

	if err := b.Build(); err != nil {
		log.Fatal("error building map: ", err)
	}

	if err := m.WriteFiles(filepath.Join(caseBuildDir, "magnacarto"+suffix)); err != nil {
		log.Fatal("error writing map: ", err)
	}
}
Example #2
0
func main() {
	mmlFilename := flag.String("mml", "", "mml file")
	var mssFilenames files

	flag.Var(&mssFilenames, "mss", "mss file")
	confFile := flag.String("config", "", "config")
	sqliteDir := flag.String("sqlite-dir", "", "sqlite directory")
	shapeDir := flag.String("shape-dir", "", "shapefile directory")
	imageDir := flag.String("image-dir", "", "image/marker directory")
	fontDir := flag.String("font-dir", "", "fonts directory")
	dumpRules := flag.Bool("dumprules", false, "print calculated rules to stderr")
	builderType := flag.String("builder", "mapnik2", "builder type {mapnik2,mapnik3,mapserver}")
	outFile := flag.String("out", "", "out file")
	deferEval := flag.Bool("deferred-eval", false, "defer variable/expression evaluation to the end")
	version := flag.Bool("version", false, "print version and exit")
	noCheckFiles := flag.Bool("no-check-files", false, "do not check if images/shps/etc exists")

	cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")

	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *version {
		fmt.Println(magnacarto.Version)
		os.Exit(0)
	}

	conf := config.Magnacarto{}
	if *confFile != "" {
		if err := conf.Load(*confFile); err != nil {
			log.Fatal(err)
		}
	}

	// overwrite config with command line args
	if *sqliteDir != "" {
		conf.Datasources.SQLiteDirs = []string{*sqliteDir}
	}
	if *fontDir != "" {
		conf.Mapnik.FontDirs = []string{*fontDir}
	}
	if *shapeDir != "" {
		conf.Datasources.ShapefileDirs = []string{*shapeDir}
	}
	if *imageDir != "" {
		conf.Datasources.ImageDirs = []string{*imageDir}
	}

	if *noCheckFiles {
		conf.Datasources.NoCheckFiles = true
	}

	locator := conf.Locator()

	var m builder.MapWriter

	switch *builderType {
	case "mapserver":
		m = mapserver.New(locator)
	case "mapnik2":
		m = mapnik.New(locator)
		m.(*mapnik.Map).SetMapnik2(true)
	case "mapnik3":
		m = mapnik.New(locator)
	default:
		log.Fatal("unknown -builder ", *builderType)
	}

	b := builder.New(m)
	if *deferEval || conf.DeferEval {
		b.EnableDeferredEval()
	}
	b.SetMML(*mmlFilename)
	for _, mss := range mssFilenames {
		b.AddMSS(mss)
	}
	if *dumpRules {
		b.SetDumpRulesDest(os.Stderr)
	}

	if err := b.Build(); err != nil {
		log.Fatal("error building map: ", err)
	}

	if *outFile == "" || *outFile == "-" {
		if err := m.Write(os.Stdout); err != nil {
			log.Fatal("error writing map to stdout: ", err)
		}
	} else {
		if err := m.WriteFiles(*outFile); err != nil {
			log.Fatal("error writing map: ", err)
		}
	}
}
Example #3
0
func build(mmlStr string, baseDir string, options C.Opts) (output, error *C.char) {
	conf := config.Magnacarto{}
	locator := conf.Locator()

	builderType := C.GoString(options.builderType)
	if builderType == "" {
		builderType = "mapnik2"
	}
	sqliteDir := C.GoString(options.sqliteDir)
	if sqliteDir != "" {
		conf.Datasources.SQLiteDirs = []string{sqliteDir}
	}
	fontDir := C.GoString(options.fontDir)
	if fontDir != "" {
		conf.Mapnik.FontDirs = []string{fontDir}
	}
	shapeDir := C.GoString(options.shapeDir)
	if shapeDir != "" {
		conf.Datasources.ShapefileDirs = []string{shapeDir}
	}
	imageDir := C.GoString(options.imageDir)
	if imageDir != "" {
		conf.Datasources.ImageDirs = []string{imageDir}
	}
	relPaths := bool(options.relPaths)
	if relPaths {
		locator.UseRelPaths(relPaths)
	}
	msNoMapBlock := bool(options.msNoMapBlock)

	locator.SetBaseDir(baseDir)
	var m builder.MapWriter

	switch builderType {
	case "mapserver":
		mm := mapserver.New(locator)
		mm.SetNoMapBlock(msNoMapBlock)
		m = mm
	case "mapnik2":
		mm := mapnik.New(locator)
		mm.SetMapnik2(true)
		m = mm
	case "mapnik3":
		m = mapnik.New(locator)
	default:
		return nil, C.CString(fmt.Sprint("unknown builder ", builderType))
	}

	r := strings.NewReader(mmlStr)
	mmlData, err := mml.Parse(r)
	if err != nil {
		return nil, C.CString(fmt.Sprint(err))
	}

	var style bytes.Buffer
	for _, s := range mmlData.Stylesheets {
		if strings.HasSuffix(s, ".mss") {
			r, err := os.Open(filepath.Join(baseDir, s))
			if err != nil {
				return nil, C.CString(fmt.Sprint(err))
			}
			content, err := ioutil.ReadAll(r)
			if err != nil {
				return nil, C.CString(fmt.Sprint(err))
			}
			r.Close()
			style.Write(content)
		} else {
			style.WriteString(s)
		}
	}

	builder.BuildMapFromString(m, mmlData, style.String())

	var buf bytes.Buffer
	if err := m.Write(&buf); err != nil {
		return nil, C.CString(fmt.Sprint(err))
	}

	return C.CString(buf.String()), nil
}
Example #4
0
func main() {
	mmlFile := flag.String("mml", "", "mml file")
	var mssFilenames files

	flag.Var(&mssFilenames, "mss", "mss file")
	confFile := flag.String("config", "", "config")
	sqliteDir := flag.String("sqlite-dir", "", "sqlite directory")
	shapeDir := flag.String("shape-dir", "", "shapefile directory")
	imageDir := flag.String("image-dir", "", "image/marker directory")
	fontDir := flag.String("font-dir", "", "fonts directory")
	dumpRules := flag.Bool("dumprules", false, "print calculated rules to stderr")
	builderType := flag.String("builder", "mapnik2", "builder type {mapnik2,mapnik3,mapserver}")
	outFile := flag.String("out", "", "out file")
	relPaths := flag.Bool("relpaths", false, "use relative paths in output style")
	version := flag.Bool("version", false, "print version and exit")

	cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")

	msNoMapBlock := flag.Bool("ms-no-map-block", false, "hide MAP block, only output layers/symbols for INCLUDE")

	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *version {
		fmt.Println(magnacarto.Version)
		os.Exit(0)
	}

	conf := config.Magnacarto{}
	if *confFile != "" {
		if err := conf.Load(*confFile); err != nil {
			log.Fatal(err)
		}
	}

	// overwrite config with command line args
	if *sqliteDir != "" {
		conf.Datasources.SQLiteDirs = []string{*sqliteDir}
	}
	if *fontDir != "" {
		conf.Mapnik.FontDirs = []string{*fontDir}
	}
	if *shapeDir != "" {
		conf.Datasources.ShapefileDirs = []string{*shapeDir}
	}
	if *imageDir != "" {
		conf.Datasources.ImageDirs = []string{*imageDir}
	}

	locator := conf.Locator()
	if *mmlFile != "" {
		locator.SetBaseDir(filepath.Dir(*mmlFile))
	}
	if *outFile != "" {
		locator.SetOutDir(filepath.Dir(*outFile))
	} else if *mmlFile != "" {
		locator.SetOutDir(filepath.Dir(*mmlFile))
	} else {
		wd, _ := os.Getwd()
		locator.SetOutDir(wd)
	}
	if *relPaths {
		locator.UseRelPaths(*relPaths)
	}

	var m builder.MapWriter

	switch *builderType {
	case "mapserver":
		mm := mapserver.New(locator)
		mm.SetNoMapBlock(*msNoMapBlock)
		m = mm
	case "mapnik2":
		mm := mapnik.New(locator)
		mm.SetMapnik2(true)
		m = mm
	case "mapnik3":
		m = mapnik.New(locator)
	default:
		log.Fatal("unknown -builder ", *builderType)
	}

	b := builder.New(m)
	b.SetMML(*mmlFile)
	for _, mss := range mssFilenames {
		b.AddMSS(mss)
	}
	if *dumpRules {
		b.SetDumpRulesDest(os.Stderr)
	}

	if err := b.Build(); err != nil {
		log.Fatal("error building style: ", err)
	}

	if *outFile == "" || *outFile == "-" {
		if err := m.Write(os.Stdout); err != nil {
			log.Fatal("error writing style to stdout: ", err)
		}
	} else {
		if err := m.WriteFiles(*outFile); err != nil {
			log.Fatal("error writing style: ", err)
		}
	}

	if mf := locator.MissingFiles(); mf != nil {
		for _, f := range mf {
			log.Println("File not found:", f)
		}
	}
}