Skip to content

Commit

Permalink
Add keeppointsandlines flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kad-dirc committed Mar 12, 2024
1 parent a47800e commit a08ef02
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 96 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,48 @@ Close thin polygons can turn into lines:
## Usage

```sh
go build .

go run . -s=[source GPKG] -t=[target GPKG] \
./texel -s=[source GPKG] -t=[target GPKG] \
-tms=[tile matrix set for filtering] -z=[tile matrix ids] \
-p=[pagesize for writing to target GPKG] -o=[overwrite target GPKG]
-p=[pagesize for writing to target GPKG] -o=[overwrite target GPKG] \
-pl=[keep points and lines]

./texel --help
```

### Docker

```docker
docker run \
--name texel \
--rm \
-u $(id -u):$(id -g) \
-v `pwd`/example:/example \
pdok/texel \
-s=./example/example.gpkg \
-t=./example/example-processed.gpkg \
-tms="NetherlandsRDNewQuad" \
-z '[5]' \
-p=10 \
-o=false \
-pl=true
```

## Build

```sh
go test ./... -covermode=atomic

go build .
```

## Docker
### Docker

```docker
docker build -t pdok/texel .
docker run --rm --name texel -v `pwd`/example:/example pdok/texel ./texel \
-s=./example/example.gpkg -t=./example/example-processed.gpkg \
-tms="NetherlandsRDNewQuad" -z '[5]' \
-p=10 -o=false
```



## References

* [sieve](https://github.com/pdok/sieve) is the predecessor of this tool
Expand Down
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const OVERWRITE string = `overwrite`
const TILEMATRIXSET string = `tilematrixset`
const TILEMATRICES string = `tilematrices`
const PAGESIZE string = `pagesize`
const KEEPPOINTSANDLINES string = `keeppointsandlines`

//nolint:funlen
func main() {
Expand Down Expand Up @@ -80,6 +81,14 @@ func main() {
Required: false,
EnvVars: []string{strcase.ToScreamingSnake(PAGESIZE)},
},
&cli.BoolFlag{
Name: KEEPPOINTSANDLINES,
Aliases: []string{"pl"},
Usage: "Parts of polygons are reduced to points and lines after texel, keep these details or not.",
Value: true,
Required: false,
EnvVars: []string{strcase.ToScreamingSnake(KEEPPOINTSANDLINES)},
},
}

app.Action = func(c *cli.Context) error {
Expand Down Expand Up @@ -107,6 +116,7 @@ func main() {
gpkgTargets := make(map[int]*gpkg.TargetGeopackage, len(tileMatrixIDs))
overwrite := c.Bool(OVERWRITE)
pagesize := c.Int(PAGESIZE) // TODO divide by tile matrices count
keepPointsAndLines := c.Bool(KEEPPOINTSANDLINES)
for _, tmID := range tileMatrixIDs {
gpkgTargets[tmID] = initGPKGTarget(targetPathFmt, tmID, overwrite, pagesize)
defer gpkgTargets[tmID].Close() // yes, supposed to go here, want to close all at end of func
Expand Down Expand Up @@ -134,7 +144,7 @@ func main() {
source.Table = table
target.Table = table
}
processBySnapping(source, targets, tileMatrixSet)
processBySnapping(source, targets, tileMatrixSet, keepPointsAndLines)
log.Printf(" finished %s", table.Name)
}

Expand Down Expand Up @@ -171,8 +181,8 @@ func injectSuffixIntoPath(p string) string {
return path.Join(dir, name+"_%v"+ext)
}

func processBySnapping(source processing.Source, targets map[tms20.TMID]processing.Target, tileMatrixSet tms20.TileMatrixSet) {
func processBySnapping(source processing.Source, targets map[tms20.TMID]processing.Target, tileMatrixSet tms20.TileMatrixSet, keepPointsAndLines bool) {
processing.ProcessFeatures(source, targets, func(p geom.Polygon, tmIDs []tms20.TMID) map[tms20.TMID][]geom.Polygon {
return snap.SnapPolygon(p, tileMatrixSet, tmIDs)
return snap.SnapPolygon(p, tileMatrixSet, tmIDs, keepPointsAndLines)
})
}
11 changes: 5 additions & 6 deletions snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (
)

const (
xAx = 0
yAx = 1
keepPointsAndLines = true // TODO do something with polys that collapsed into points and lines
xAx = 0
yAx = 1
)

type IsOuter = bool
Expand All @@ -34,7 +33,7 @@ type IsOuter = bool
// and adds points to lines to prevent intersections.
//
//nolint:revive
func SnapPolygon(polygon geom.Polygon, tileMatrixSet tms20.TileMatrixSet, tmIDs []tms20.TMID) map[tms20.TMID][]geom.Polygon {
func SnapPolygon(polygon geom.Polygon, tileMatrixSet tms20.TileMatrixSet, tmIDs []tms20.TMID, keepPointsAndLines bool) map[tms20.TMID][]geom.Polygon {
deepestID := slices.Max(tmIDs)
ix := pointindex.FromTileMatrixSet(tileMatrixSet, deepestID)
tmIDsByLevels := tileMatrixIDsByLevels(tileMatrixSet, tmIDs)
Expand All @@ -44,7 +43,7 @@ func SnapPolygon(polygon geom.Polygon, tileMatrixSet tms20.TileMatrixSet, tmIDs
}

ix.InsertPolygon(polygon)
newPolygonsPerLevel := addPointsAndSnap(ix, polygon, levels)
newPolygonsPerLevel := addPointsAndSnap(ix, polygon, levels, keepPointsAndLines)

newPolygonsPerTileMatrixID := make(map[tms20.TMID][]geom.Polygon, len(newPolygonsPerLevel))
for level, newPolygons := range newPolygonsPerLevel {
Expand All @@ -67,7 +66,7 @@ func tileMatrixIDsByLevels(tms tms20.TileMatrixSet, tmIDs []tms20.TMID) map[poin
}

//nolint:cyclop
func addPointsAndSnap(ix *pointindex.PointIndex, polygon geom.Polygon, levels []pointindex.Level) map[pointindex.Level][]geom.Polygon {
func addPointsAndSnap(ix *pointindex.PointIndex, polygon geom.Polygon, levels []pointindex.Level, keepPointsAndLines bool) map[pointindex.Level][]geom.Polygon {
levelMap := mapslicehelp.AsKeys(levels)
newOuters := make(map[pointindex.Level][][][2]float64, len(levels))
newInners := make(map[pointindex.Level][][][2]float64, len(levels))
Expand Down
Loading

0 comments on commit a08ef02

Please sign in to comment.