예제 #1
0
// BuildMapFromString parses the style from a string and adds all
// mml.Layers to the map.
func BuildMapFromString(m Map, mml *mml.MML, style string) error {
	carto := mss.New()

	err := carto.ParseString(style)
	if err != nil {
		return err
	}
	if err := carto.Evaluate(); err != nil {
		return err
	}

	for _, l := range mml.Layers {
		rules := carto.MSS().LayerRules(l.ID, l.Classes...)

		if len(rules) > 0 {
			m.AddLayer(l, rules)
		}
	}

	if m, ok := m.(MapOptionsSetter); ok {
		if bgColor, ok := carto.MSS().Map().GetColor("background-color"); ok {
			m.SetBackgroundColor(bgColor)
		}
	}
	return nil
}
예제 #2
0
파일: builder.go 프로젝트: kaiCu/magnacarto
// Build parses MML, MSS files, builds all rules and adds them to the Map.
func (b *Builder) Build() error {
	layerNames := []string{}
	layers := []mml.Layer{}

	if b.mml != "" {
		r, err := os.Open(b.mml)
		if err != nil {
			return err
		}
		defer r.Close()
		mml, err := mml.Parse(r)
		if err != nil {
			return err
		}
		if len(b.mss) == 0 {
			for _, s := range mml.Stylesheets {
				b.mss = append(b.mss, filepath.Join(filepath.Dir(b.mml), s))
			}
		}

		for _, l := range mml.Layers {
			layers = append(layers, l)
			layerNames = append(layerNames, l.Name)
		}
	}

	carto := mss.New()
	if b.deferEval {
		carto.EnableDeferredEval()
	}

	for _, mss := range b.mss {
		err := carto.ParseFile(mss)
		if err != nil {
			return err
		}
	}

	if err := carto.Evaluate(); err != nil {
		return err
	}

	if b.mml == "" {
		layerNames = carto.MSS().Layers()
		for _, layerName := range layerNames {
			layers = append(layers,
				// XXX assume we only have LineStrings for -mss only export
				mml.Layer{Name: layerName, Type: mml.LineString},
			)
		}
	}

	for _, l := range layers {
		rules := carto.MSS().LayerRules(l.Name, l.Classes...)

		if b.dumpRules != nil {
			for _, r := range rules {
				fmt.Fprintln(b.dumpRules, r.String())
			}
		}
		if len(rules) > 0 {
			b.dstMap.AddLayer(l, rules)
		}
	}

	if m, ok := b.dstMap.(MapOptionsSetter); ok {
		if bgColor, ok := carto.MSS().Map().GetColor("background-color"); ok {
			m.SetBackgroundColor(bgColor)
		}
	}
	return nil
}