WKT Geofence Lookup

medium geometry parsing math 2000 ms · 256 MB

WKT Geofence Lookup

A fleet-tracking system stores its geofences in a tiny subset of Well-Known Text (WKT). Each fence is either a POLYGON (one outer ring plus zero or more holes) or a MULTIPOLYGON (several such polygons grouped under one name). You must parse the fences and then, for each query point, report which fences contain it — and whether the point lands exactly on a fence's boundary.

Input

The first line contains two integers f and q (1 ≤ f ≤ 30, 1 ≤ q ≤ 1000): the number of fences and the number of query points.

Each of the next f lines defines one fence, matching this grammar (WS is zero or more spaces and/or tabs, WS+ is one or more):

line        := WS name WS "=" WS geometry WS
name        := letter (letter | digit | "_")*          (1 to 20 characters)
geometry    := "POLYGON" WS ringList
             | "MULTIPOLYGON" WS multiBody
ringList    := "(" WS ring (WS "," WS ring)* WS ")"
multiBody   := "(" WS ringList (WS "," WS ringList)* WS ")"
ring        := "(" WS point (WS "," WS point)* WS ")"
point       := integer WS+ integer
integer     := "-"? digit+                              (no leading zeros, except "0" itself)
  • letter is a–z or A–Z; digit is 0–9.
  • Whitespace (spaces and tabs) may appear before or after =, the keyword, any (, ) or ,, and at the start or end of the line — except that the x and y of a single point are always separated by at least one space or tab (that separator is mandatory, since otherwise the two numbers couldn't be told apart).
  • POLYGON and MULTIPOLYGON are written exactly like that, in upper case.
  • Every ring has at least 4 points, and its first and last points are identical (rings are always closed explicitly in the input — you don't need to verify or add this).
  • In a POLYGON, the first ring is the outer boundary and every later ring is a hole cut out of it. In a MULTIPOLYGON, each ringList is one such polygon (outer ring plus its own holes, possibly none), and the fence is the union of all of them.
  • Coordinates are integers with absolute value at most 10^6.
  • Fence names consist only of letters, digits and underscores, start with a letter, are at most 20 characters long, and are distinct across the whole input.
  • Every ring is simple (its edges don't cross or touch except at shared endpoints of consecutive edges). A polygon's holes lie inside its outer ring and don't overlap each other or the outer boundary (though a hole may touch the outer ring or another hole, sharing single points or whole edges). The polygons making up one MULTIPOLYGON never overlap each other.
  • The total number of points over every ring of every fence (each ring's repeated closing point counts once per occurrence) is at most 2000.

Each of the next q lines contains two integers x and y (|x|, |y| ≤ 10^6), separated by a single space: one query point.

Output

For each query point, in input order, print one line. A fence contains the point if the point is inside the fenced area or lies exactly on any of its ring boundaries (outer ring or a hole's edge); being on any boundary is reported as BOUNDARY even when the point would otherwise be strictly inside, and a point strictly inside a hole (not on the hole's edge) counts as outside that fence, since holes remove area.

Print the number of fences that contain the point, followed by name:STATUS for each of those fences — in the order the fences were defined in the input — where STATUS is INSIDE or BOUNDARY. Separate all tokens on the line with single spaces. If no fence contains the point, print just 0.

Notes

In the first example, Pen is a 10×10 square. The point (5, 5) is strictly inside it, (0, 5) lies on its left edge, (10, 10) is a corner (still BOUNDARY), and (15, 5) is outside.

In the second example, Ring is a 10×10 square with a 3×3 hole square inside it, and Split is a MULTIPOLYGON made of two disjoint squares far from Ring. (5, 5) sits strictly inside Ring's hole and outside both of Split's squares, so no fence contains it. (3, 5) lies exactly on the hole's left edge, a BOUNDARY hit on Ring. (8, 8) is inside Ring (outside the hole) but misses both of Split's squares. (16, 16) is inside the second square of Split, even though Ring and the first square of Split both miss it.

Example 1

Input

1 4
Pen = POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))
5 5
0 5
10 10
15 5

Output

1 Pen:INSIDE
1 Pen:BOUNDARY
1 Pen:BOUNDARY
0

Example 2

Input

2 4
Ring = POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(3 3, 7 3, 7 7, 3 7, 3 3))
Split = MULTIPOLYGON(((0 0, 2 0, 2 2, 0 2, 0 0)), ((15 15, 18 15, 18 18, 15 18, 15 15)))
5 5
3 5
8 8
16 16

Output

0
1 Ring:BOUNDARY
1 Ring:INSIDE
1 Split:INSIDE

Hints · reveal one at a time if you're stuck

Hint 1

The input is small: at most 1000 queries against at most 2000 vertices in total. Once the rings are parsed, is it fine to test each query against every edge of every fence?

Hint 2

Treat every ring of a fence the same way, since holes and separate polygons can be handled by counting crossings. A point that lies inside a hole crosses an even number of edges, so the parity of crossings already reflects the hole.

Hint 3

Check the boundary first with exact integer arithmetic: a point is on an edge when the cross product is zero and it lies within the edge's bounding box. For the inside test, use the half-open rule that counts an edge only when its two endpoints lie on opposite sides of the query's y, so a ray through a vertex is counted once.

report an issue Prefer your own editor? sooks prep 64 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.