Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set tzbounds to limit number of tzdata polygons to evaluate #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions localtimezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,18 @@ type LocalTimeZone interface {
}

type tzData struct {
tzid string
polygon *orb.Polygon
multiPolygon *orb.MultiPolygon
bound *orb.Bound
centers []orb.Point
}

type localTimeZone struct {
tzids []string
tzData map[string]tzData
mu sync.RWMutex
tzids []string
tzData map[string]tzData
tzBounds map[float64][]tzData
mu sync.RWMutex
}

var _ LocalTimeZone = &localTimeZone{}
Expand Down Expand Up @@ -158,14 +160,13 @@ func (z *localTimeZone) getZone(point Point, single bool) (tzids []string, err e
}
z.mu.RLock()
defer z.mu.RUnlock()
for _, id := range z.tzids {
d := z.tzData[id]
for _, d := range z.tzBounds[math.Floor(point.Lon)] {
if !d.bound.Contains(p) {
continue
}
if d.polygon != nil {
if planar.PolygonContains(*d.polygon, p) {
tzids = append(tzids, id)
tzids = append(tzids, d.tzid)
if single {
return
}
Expand All @@ -174,7 +175,7 @@ func (z *localTimeZone) getZone(point Point, single bool) (tzids []string, err e
}
if d.multiPolygon != nil {
if planar.MultiPolygonContains(*d.multiPolygon, p) {
tzids = append(tzids, id)
tzids = append(tzids, d.tzid)
if single {
return
}
Expand Down Expand Up @@ -230,7 +231,9 @@ func (z *localTimeZone) buildCache(features []*geojson.Feature) {
defer wg.Done()
id := f.Properties.MustString("tzid")
var multiPolygon orb.MultiPolygon
d := tzData{}
d := tzData{
tzid: id,
}
polygon, ok := f.Geometry.(orb.Polygon)
if ok {
d.polygon = &polygon
Expand All @@ -251,6 +254,9 @@ func (z *localTimeZone) buildCache(features []*geojson.Feature) {
d.centers = tzCenters
m.Lock()
z.tzData[id] = d
for lon := math.Floor(bound.Left()); lon < math.Ceil(bound.Right()); lon += 1 {
z.tzBounds[lon] = append(z.tzBounds[lon], d)
}
m.Unlock()
}(f)
}
Expand All @@ -264,6 +270,9 @@ func (z *localTimeZone) buildCache(features []*geojson.Feature) {
i++
}
sort.Strings(z.tzids)
for _, tzDatas := range z.tzBounds {
sort.Slice(tzDatas, func(i, j int) bool { return tzDatas[i].tzid < tzDatas[j].tzid })
}
}

// LoadGeoJSON loads a custom GeoJSON shapefile from a Reader
Expand All @@ -284,6 +293,7 @@ func (z *localTimeZone) LoadGeoJSON(r io.Reader) error {
}
z.tzData = make(map[string]tzData, TZCount) // Possibly the incorrect length in case of Mock or custom data
z.tzids = []string{} // Cannot set a length or else array will be full of empty strings
z.tzBounds = make(map[float64][]tzData)
go func(features []*geojson.Feature) {
defer z.mu.Unlock()
z.buildCache(features)
Expand Down