Skip to content

Commit

Permalink
Merge branch 'master' into render-feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
olt committed Aug 8, 2017
2 parents dc0204d + f916062 commit a20ec45
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 142 deletions.
14 changes: 11 additions & 3 deletions builder/mapnik/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"os"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -140,6 +141,9 @@ func (m *Map) WriteFiles(basename string) error {
return m.Write(f)
}

// whether a string is a connection (PG:xxx) or filename
var isOgrConnection = regexp.MustCompile(`^[a-zA-Z]{2,}:`)

func (m *Map) newDatasource(ds mml.Datasource, rules []mss.Rule) []Parameter {
var params []Parameter
switch ds := ds.(type) {
Expand Down Expand Up @@ -174,8 +178,12 @@ func (m *Map) newDatasource(ds mml.Datasource, rules []mss.Rule) []Parameter {
{Name: "type", Value: "sqlite"},
}
case mml.OGR:
file := ds.Filename
if !isOgrConnection.MatchString(ds.Filename) {
file = m.locator.Data(ds.Filename)
}
params = []Parameter{
{Name: "file", Value: ds.Filename},
{Name: "file", Value: file},
{Name: "srid", Value: ds.SRID},
{Name: "extent", Value: ds.Extent},
{Name: "layer", Value: ds.Layer},
Expand All @@ -184,7 +192,7 @@ func (m *Map) newDatasource(ds mml.Datasource, rules []mss.Rule) []Parameter {
}
case mml.GDAL:
params = []Parameter{
{Name: "file", Value: ds.Filename},
{Name: "file", Value: m.locator.Data(ds.Filename)},
{Name: "srid", Value: ds.SRID},
{Name: "extent", Value: ds.Extent},
{Name: "band", Value: ds.Band},
Expand All @@ -197,7 +205,7 @@ func (m *Map) newDatasource(ds mml.Datasource, rules []mss.Rule) []Parameter {
{Name: "type", Value: "geojson"},
}
case nil:
// datasource might be nil for exports withour mml
// datasource might be nil for exports without mml
default:
panic(fmt.Sprintf("datasource not supported by Mapnik: %v", ds))
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/magnacarto/magnacarto.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func main() {
shapeDir := flag.String("shape-dir", "", "shapefile directory")
imageDir := flag.String("image-dir", "", "image/marker directory")
fontDir := flag.String("font-dir", "", "fonts directory")
dataDir := flag.String("data-dir", "", "data directory for OGR/GDAL files, also fallback for sqlite/shape/image/font-dir")
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")
Expand Down Expand Up @@ -72,16 +73,19 @@ func main() {

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

locator := conf.Locator()
Expand Down
112 changes: 75 additions & 37 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Datasource struct {
ShapefileDirs []string `toml:"shapefile_dirs"`
SQLiteDirs []string `toml:"sqlite_dirs"`
ImageDirs []string `toml:"image_dirs"`
DataDirs []string `toml:"data_dirs"`
FontDirs []string `toml:"font_dirs"`
}

type PostGIS struct {
Expand All @@ -45,6 +47,7 @@ type Locator interface {
SQLite(string) string
Shape(string) string
Image(string) string
Data(string) string
PostGIS(mml.PostGIS) mml.PostGIS
SetBaseDir(string)
SetOutDir(string)
Expand All @@ -68,6 +71,7 @@ func Load(fileName string) (*Magnacarto, error) {
if !filepath.IsAbs(config.OutDir) {
config.OutDir = filepath.Join(config.BaseDir, config.OutDir)
}

return &config, nil
}

Expand Down Expand Up @@ -99,7 +103,16 @@ func (m *Magnacarto) Locator() Locator {
}
locator.AddShapeDir(dir)
}
for _, dir := range m.Mapnik.FontDirs {
for _, dir := range m.Datasources.DataDirs {
if !filepath.IsAbs(dir) {
dir = filepath.Join(m.BaseDir, dir)
}
locator.AddDataDir(dir)
}

fontDirs := append([]string{}, m.Mapnik.FontDirs...)
fontDirs = append(fontDirs, m.Datasources.FontDirs...)
for _, dir := range fontDirs {
if !filepath.IsAbs(dir) {
dir = filepath.Join(m.BaseDir, dir)
}
Expand All @@ -114,6 +127,7 @@ type LookupLocator struct {
sqliteDirs []string
shapeDirs []string
imageDirs []string
dataDirs []string
pgConfig *PostGIS
baseDir string
outDir string
Expand All @@ -133,33 +147,8 @@ func (l *LookupLocator) UseRelPaths(rel bool) {
l.relative = rel
}

func (l *LookupLocator) find(basename string, dirs []string) (fname string, ok bool) {
defer func() {
if fname == "" {
if l.missing == nil {
l.missing = make(map[string]struct{})
}
l.missing[basename] = struct{}{}
fname = basename
} else {
absfname, err := filepath.Abs(fname)
if err == nil {
fname = absfname
}
}

if l.relative {
relfname, err := filepath.Rel(l.outDir, fname)
if err == nil {
fname = relfname
}
} else {
if !filepath.IsAbs(fname) { // for missing files
fname = filepath.Join(l.outDir, fname)
}
}
}()

func (l *LookupLocator) find(basename string, dirs []string) (string, bool) {
// helper func: check if basename exists in dir
check := func(dir string) string {
fname := filepath.Join(dir, basename)
if _, err := os.Stat(fname); err == nil {
Expand All @@ -168,22 +157,63 @@ func (l *LookupLocator) find(basename string, dirs []string) (fname string, ok b
return ""
}

if filepath.IsAbs(basename) {
if fname := check(""); fname != "" {
return fname, true
// check for file in different dirs, uses closure so that
// we can return if we found the file
fname, ok := func() (string, bool) {
// check without any dir if it's an absolute path
if filepath.IsAbs(basename) {
if fname := check(""); fname != "" {
return fname, true
}
}
}

for _, d := range dirs {
if fname := check(d); fname != "" {
// check passed dirs
for _, d := range dirs {
if fname := check(d); fname != "" {
return fname, true
}
}
// check data dirs
for _, d := range l.dataDirs {
if fname := check(d); fname != "" {
return fname, true
}
}

// at last check with basedir
if fname := check(l.baseDir); fname != "" {
return fname, true
}

return "", false
}()

if !ok {
// register as missing file
if l.missing == nil {
l.missing = make(map[string]struct{})
}
l.missing[basename] = struct{}{}
fname = basename
} else {
absfname, err := filepath.Abs(fname)
if err == nil {
fname = absfname
}
}
if fname := check(l.baseDir); fname != "" {
return fname, true

if l.relative {
relfname, err := filepath.Rel(l.outDir, fname)
if err == nil {
fname = relfname
}
} else {
if !filepath.IsAbs(fname) { // for missing files
fname = filepath.Join(l.outDir, fname)
}
}
return fname, ok

return "", false
}

func (l *LookupLocator) AddFontDir(dir string) {
Expand All @@ -198,6 +228,9 @@ func (l *LookupLocator) AddShapeDir(dir string) {
func (l *LookupLocator) AddImageDir(dir string) {
l.imageDirs = append(l.imageDirs, dir)
}
func (l *LookupLocator) AddDataDir(dir string) {
l.dataDirs = append(l.dataDirs, dir)
}
func (l *LookupLocator) SetPGConfig(pgConfig PostGIS) {
l.pgConfig = &pgConfig
}
Expand Down Expand Up @@ -227,6 +260,11 @@ func (l *LookupLocator) Image(basename string) string {
fname, _ := l.find(basename, l.imageDirs)
return fname
}
func (l *LookupLocator) Data(basename string) string {
fname, _ := l.find(basename, nil) // dataDir is already searched by l.find
return fname
}

func (l *LookupLocator) PostGIS(ds mml.PostGIS) mml.PostGIS {
if l.pgConfig == nil {
return ds
Expand Down
Loading

0 comments on commit a20ec45

Please sign in to comment.