-
Notifications
You must be signed in to change notification settings - Fork 8
feat(wkb): add MultiPolygon support to WKB parser #44
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
Open
wietzesuijker
wants to merge
6
commits into
geoarrow:main
Choose a base branch
from
wietzesuijker:feat/multipolygon-wkb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
25d658c
Parse WKB to GeoArrow
kylebarron 1183301
update type imports (#30)
smohiudd 6ca92e2
Consolidate apache arrow imports (#33)
hanbyul-here 77d2fcf
fix polygon ringoffset (#32)
smohiudd da33f57
feat(wkb): MultiPolygon support in WKB parser
wietzesuijker 890090a
feat(wkb): LargeWKB type
wietzesuijker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { parseWkb, WKBType } from "./wkb.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import { makeData, Binary, Data, List } from "apache-arrow"; | ||
| import { parseWkb, WKBType } from "./wkb"; | ||
|
|
||
| function hexToUint8Array(hex: string): Uint8Array { | ||
| const bytes = new Uint8Array(hex.length / 2); | ||
| for (let i = 0; i < hex.length; i += 2) { | ||
| bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16); | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
| function makeWkbData(...hexStrings: string[]) { | ||
| const buffers = hexStrings.map(hexToUint8Array); | ||
| const totalLen = buffers.reduce((s, b) => s + b.length, 0); | ||
| const wkb = new Uint8Array(totalLen); | ||
| const valueOffsets = new Int32Array(hexStrings.length + 1); | ||
| let offset = 0; | ||
| for (let i = 0; i < buffers.length; i++) { | ||
| wkb.set(buffers[i], offset); | ||
| offset += buffers[i].length; | ||
| valueOffsets[i + 1] = offset; | ||
| } | ||
| return makeData({ | ||
| type: new Binary(), | ||
| data: wkb, | ||
| valueOffsets, | ||
| }); | ||
| } | ||
|
|
||
| /** Extract offset arrays from nested List data for structural assertions */ | ||
| function getOffsets(data: Data<List>, level: number): Int32Array { | ||
| let d: Data = data; | ||
| for (let i = 0; i < level; i++) { | ||
| d = (d as Data<List>).children[0]; | ||
| } | ||
| return (d as Data<List>).valueOffsets; | ||
| } | ||
|
|
||
| function getCoords(data: Data<List>, dim: number): number[][] { | ||
| let d: Data = data; | ||
| // walk: geom -> polygon -> ring -> vertex (FixedSizeList) -> Float64 | ||
| while (d.children?.length) { | ||
| d = d.children[0]; | ||
| } | ||
| const flat = d.values as Float64Array; | ||
| const coords: number[][] = []; | ||
| for (let i = 0; i < flat.length; i += dim) { | ||
| coords.push(Array.from(flat.slice(i, i + dim))); | ||
| } | ||
| return coords; | ||
| } | ||
|
|
||
| // Generated via shapely: | ||
| // MultiPolygon([box(0,0,1,1), box(2,2,3,3)]).wkb_hex | ||
| const TWO_SQUARES_HEX = | ||
| "01060000000200000001030000000100000005000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000000000000000F03F0000000000000000010300000001000000050000000000000000000840000000000000004000000000000008400000000000000840000000000000004000000000000008400000000000000040000000000000004000000000000008400000000000000040"; | ||
|
|
||
| // MultiPolygon([box(0,0,1,1)]).wkb_hex | ||
| const SINGLE_POLYGON_HEX = | ||
| "01060000000100000001030000000100000005000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000000000000000F03F0000000000000000"; | ||
|
|
||
| // MultiPolygon([Polygon(box(0,0,10,10).exterior, [box(2,2,4,4).exterior])]).wkb_hex | ||
| const WITH_HOLE_HEX = | ||
| "010600000001000000010300000002000000050000000000000000002440000000000000000000000000000024400000000000002440000000000000000000000000000024400000000000000000000000000000000000000000000024400000000000000000050000000000000000001040000000000000004000000000000010400000000000001040000000000000004000000000000010400000000000000040000000000000004000000000000010400000000000000040"; | ||
|
|
||
| describe("parseWkb MultiPolygon", () => { | ||
| test("two squares: structure and coordinates", () => { | ||
| const wkbData = makeWkbData(TWO_SQUARES_HEX); | ||
| const result = parseWkb(wkbData, WKBType.MultiPolygon, 2) as Data<List>; | ||
| expect(result.length).toBe(1); | ||
|
|
||
| // geom offsets: 1 multipolygon containing 2 polygons | ||
| const geomOffsets = result.valueOffsets; | ||
| expect(Array.from(geomOffsets)).toEqual([0, 2]); | ||
|
|
||
| // polygon offsets: 2 polygons, each with 1 ring | ||
| const polyOffsets = getOffsets(result, 1); | ||
| expect(Array.from(polyOffsets)).toEqual([0, 1, 2]); | ||
|
|
||
| // ring offsets: 2 rings, each with 5 coords (closed box) | ||
| const ringOffsets = getOffsets(result, 2); | ||
| expect(Array.from(ringOffsets)).toEqual([0, 5, 10]); | ||
|
|
||
| // verify coordinate values | ||
| const coords = getCoords(result, 2); | ||
| expect(coords.length).toBe(10); | ||
| // first polygon starts at (1,0) - shapely box winding | ||
| expect(coords[0]).toEqual([1, 0]); | ||
| // second polygon starts at (3,2) | ||
| expect(coords[5]).toEqual([3, 2]); | ||
| }); | ||
|
|
||
| test("single polygon in multipolygon", () => { | ||
| const wkbData = makeWkbData(SINGLE_POLYGON_HEX); | ||
| const result = parseWkb(wkbData, WKBType.MultiPolygon, 2) as Data<List>; | ||
| expect(result.length).toBe(1); | ||
|
|
||
| const geomOffsets = result.valueOffsets; | ||
| expect(Array.from(geomOffsets)).toEqual([0, 1]); | ||
|
|
||
| const polyOffsets = getOffsets(result, 1); | ||
| expect(Array.from(polyOffsets)).toEqual([0, 1]); | ||
|
|
||
| const ringOffsets = getOffsets(result, 2); | ||
| expect(Array.from(ringOffsets)).toEqual([0, 5]); | ||
|
|
||
| expect(getCoords(result, 2).length).toBe(5); | ||
| }); | ||
|
|
||
| test("polygon with hole: 2 rings", () => { | ||
| const wkbData = makeWkbData(WITH_HOLE_HEX); | ||
| const result = parseWkb(wkbData, WKBType.MultiPolygon, 2) as Data<List>; | ||
| expect(result.length).toBe(1); | ||
|
|
||
| // 1 multipolygon -> 1 polygon -> 2 rings (outer + hole) | ||
| const geomOffsets = result.valueOffsets; | ||
| expect(Array.from(geomOffsets)).toEqual([0, 1]); | ||
|
|
||
| const polyOffsets = getOffsets(result, 1); | ||
| expect(Array.from(polyOffsets)).toEqual([0, 2]); | ||
|
|
||
| const ringOffsets = getOffsets(result, 2); | ||
| expect(Array.from(ringOffsets)).toEqual([0, 5, 10]); | ||
|
|
||
| const coords = getCoords(result, 2); | ||
| expect(coords.length).toBe(10); | ||
| // outer ring starts at (10,0), hole starts at (4,2) | ||
| expect(coords[0]).toEqual([10, 0]); | ||
| expect(coords[5]).toEqual([4, 2]); | ||
| }); | ||
|
|
||
| test("batch of multiple multipolygon features", () => { | ||
| const wkbData = makeWkbData(TWO_SQUARES_HEX, SINGLE_POLYGON_HEX); | ||
| const result = parseWkb(wkbData, WKBType.MultiPolygon, 2) as Data<List>; | ||
| expect(result.length).toBe(2); | ||
|
|
||
| // feature 0 has 2 polygons, feature 1 has 1 polygon | ||
| const geomOffsets = result.valueOffsets; | ||
| expect(Array.from(geomOffsets)).toEqual([0, 2, 3]); | ||
|
|
||
| // 3 polygons total, each with 1 ring | ||
| const polyOffsets = getOffsets(result, 1); | ||
| expect(Array.from(polyOffsets)).toEqual([0, 1, 2, 3]); | ||
|
|
||
| // 3 rings, each with 5 coords | ||
| const ringOffsets = getOffsets(result, 2); | ||
| expect(Array.from(ringOffsets)).toEqual([0, 5, 10, 15]); | ||
|
|
||
| expect(getCoords(result, 2).length).toBe(15); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely wouldn't want to depend on
loaders.glfromgeoarrow-js.