Skip to content

Commit

Permalink
Adjustments to Polylabel
Browse files Browse the repository at this point in the history
* mv new functions to existing geo/util
* add multipoly support (will use the largest poly)
* make the arrays with their known lengths
* fix up imports
  • Loading branch information
comstud committed Mar 7, 2024
1 parent ec4cfda commit f2e12f7
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 53 deletions.
50 changes: 50 additions & 0 deletions geo/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"fmt"
"strconv"

venise_geo "github.com/dernise/venise/geo"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geo"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
"gopkg.in/guregu/null.v4"
)

Expand Down Expand Up @@ -62,3 +66,49 @@ func NameAndIntIdFromFeature(feature *geojson.Feature) (string, null.String, int

return name, areaName, nestId, nil
}

func convertToVenisePolygon(orbPolygon orb.Polygon) venise_geo.Polygon {
polygon := venise_geo.Polygon{
Rings: make([][]venise_geo.Point, len(orbPolygon)),
}
for ringIdx, ring := range orbPolygon {
ringPoints := make([]venise_geo.Point, len(ring))
for ptsIdx, coord := range ring {
ringPoints[ptsIdx] = venise_geo.Point(coord)
}
polygon.Rings[ringIdx] = ringPoints
}
return polygon
}

func GetPolygonLabelPoint(geometry orb.Geometry) orb.Point {
center, _ := planar.CentroidArea(geometry)
switch typedGeometry := geometry.(type) {
case orb.Polygon:
if !planar.PolygonContains(typedGeometry, center) {
point := venise_geo.Polylabel(convertToVenisePolygon(typedGeometry), 0.000001, false)
return orb.Point(point)
}
case orb.MultiPolygon:
if !planar.MultiPolygonContains(typedGeometry, center) {
if len(typedGeometry) < 1 {
break
}

bestPoly := typedGeometry[0]
maxArea := geo.Area(bestPoly)

for _, poly := range typedGeometry[1:] {
area := geo.Area(poly)
if area > maxArea {
maxArea = area
bestPoly = poly
}
}

point := venise_geo.Polylabel(convertToVenisePolygon(bestPoly), 0.000001, false)
return orb.Point(point)
}
}
return center
}
5 changes: 3 additions & 2 deletions importer/exporters/overpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package exporters
import (
"context"
"fmt"
"github.com/UnownHash/Fletchling/util"

"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
"github.com/paulmach/osm/osmgeojson"
"github.com/sirupsen/logrus"

"github.com/UnownHash/Fletchling/geo"
"github.com/UnownHash/Fletchling/overpass"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func (exporter *OverpassExporter) ExportFeatures(ctx context.Context) ([]*geojso

for _, feature := range fc.Features {
geometry := feature.Geometry
featureCenter := util.GetPolygonLabelPoint(geometry)
featureCenter := geo.GetPolygonLabelPoint(geometry)

overpass.AdjustFeatureProperties(feature)

Expand Down
11 changes: 5 additions & 6 deletions importer/importers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package importers
import (
"context"
"encoding/json"
"github.com/UnownHash/Fletchling/util"
"time"

"github.com/paulmach/orb/geo"
orb_geo "github.com/paulmach/orb/geo"
"github.com/paulmach/orb/geojson"
"github.com/sirupsen/logrus"
"gopkg.in/guregu/null.v4"

"github.com/UnownHash/Fletchling/db_store"
np_geo "github.com/UnownHash/Fletchling/geo"
"github.com/UnownHash/Fletchling/geo"
)

type DBImporter struct {
Expand All @@ -28,7 +27,7 @@ func (importer *DBImporter) ImportFeatures(ctx context.Context, features []*geoj
nowEpoch := time.Now().Unix()

for _, feature := range features {
name, areaName, nestId, err := np_geo.NameAndIntIdFromFeature(feature)
name, areaName, nestId, err := geo.NameAndIntIdFromFeature(feature)
if err != nil {
importer.logger.Warnf("DBImporter: skipping feature: %v", err)
continue
Expand All @@ -40,7 +39,7 @@ func (importer *DBImporter) ImportFeatures(ctx context.Context, features []*geoj
}

geometry := feature.Geometry
area := geo.Area(geometry)
area := orb_geo.Area(geometry)

existingNest, _ := importer.nestsDBStore.GetNestByID(ctx, nestId)

Expand Down Expand Up @@ -77,7 +76,7 @@ func (importer *DBImporter) ImportFeatures(ctx context.Context, features []*geoj
updated = null.IntFrom(nowEpoch)
}

center := util.GetPolygonLabelPoint(geometry)
center := geo.GetPolygonLabelPoint(geometry)

nest := &db_store.Nest{
NestId: nestId,
Expand Down
7 changes: 3 additions & 4 deletions importer/importers/koji.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"errors"
"fmt"
"github.com/UnownHash/Fletchling/util"
"strconv"

"github.com/paulmach/orb/geojson"
"github.com/sirupsen/logrus"

np_geo "github.com/UnownHash/Fletchling/geo"
"github.com/UnownHash/Fletchling/geo"
"github.com/UnownHash/Fletchling/koji_client"
)

Expand All @@ -37,7 +36,7 @@ func looksLikeNumber(s string) bool {
}

func (importer *KojiImporter) importFeature(ctx context.Context, feature *geojson.Feature, projects []int, geofencesByName map[string]*koji_client.Geofence, geofencesByNestId map[int64]*koji_client.Geofence) *koji_client.Geofence {
name, areaName, nestId, err := np_geo.NameAndIntIdFromFeature(feature)
name, areaName, nestId, err := geo.NameAndIntIdFromFeature(feature)
if err != nil {
importer.logger.Warnf("KojiImporter: skipping feature: %v", err)
return nil
Expand All @@ -62,7 +61,7 @@ func (importer *KojiImporter) importFeature(ctx context.Context, feature *geojso
return name
}

featureCenter := util.GetPolygonLabelPoint(feature.Geometry)
featureCenter := geo.GetPolygonLabelPoint(feature.Geometry)

altName := name + fmt.Sprintf(" at %0.5f,%0.5f", featureCenter.Lat(), featureCenter.Lon())

Expand Down
12 changes: 6 additions & 6 deletions importer/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package importer
import (
"context"
"fmt"
"github.com/UnownHash/Fletchling/util"
"github.com/paulmach/orb/geo"

"github.com/UnownHash/Fletchling/geo"
orb_geo "github.com/paulmach/orb/geo"
"github.com/paulmach/orb/geojson"
"github.com/sirupsen/logrus"

np_geo "github.com/UnownHash/Fletchling/geo"
"github.com/UnownHash/Fletchling/importer/exporters"
"github.com/UnownHash/Fletchling/importer/importers"
)
Expand Down Expand Up @@ -45,14 +45,14 @@ func (runner *ImportRunner) Import(ctx context.Context) error {
}
name = config.DefaultName
if config.DefaultNameLocation {
centroid := util.GetPolygonLabelPoint(feature.Geometry)
centroid := geo.GetPolygonLabelPoint(feature.Geometry)

name += fmt.Sprintf(" at %0.5f,%0.5f", centroid.Lat(), centroid.Lon())
}
feature.Properties["name"] = name
}

name, areaName, _, err := np_geo.NameAndIntIdFromFeature(feature)
name, areaName, _, err := geo.NameAndIntIdFromFeature(feature)
if err != nil {
// exporters should deal with some of this, so only logging debug.
runner.logger.Debugf("ImportRunner: skipping feature: %v", err)
Expand All @@ -65,7 +65,7 @@ func (runner *ImportRunner) Import(ctx context.Context) error {
}

geometry := feature.Geometry
area := geo.Area(geometry)
area := orb_geo.Area(geometry)

if area < config.MinAreaM2 {
runner.logger.Warnf(
Expand Down
8 changes: 4 additions & 4 deletions processor/models/nest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package models
import (
"encoding/json"
"fmt"
"github.com/UnownHash/Fletchling/util"
"strconv"
"sync"
"time"

"github.com/paulmach/orb"
"github.com/paulmach/orb/geo"
orb_geo "github.com/paulmach/orb/geo"
"github.com/paulmach/orb/geojson"
"gopkg.in/guregu/null.v4"

"github.com/UnownHash/Fletchling/db_store"
"github.com/UnownHash/Fletchling/geo"
)

// NestingPokemonInfo contains info about a nesting pokemon. 'Count'
Expand Down Expand Up @@ -322,9 +322,9 @@ func NewNestFromKojiFeature(feature *geojson.Feature) (*Nest, error) {

geometry := feature.Geometry
jsonGeometry := geojson.NewGeometry(geometry)
center := util.GetPolygonLabelPoint(geometry)
center := geo.GetPolygonLabelPoint(geometry)

area := geo.Area(geometry)
area := orb_geo.Area(geometry)

return &Nest{
Id: nestId,
Expand Down
31 changes: 0 additions & 31 deletions util/geo.go

This file was deleted.

0 comments on commit f2e12f7

Please sign in to comment.