-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deno lint fixes, Polygon implementation
- Loading branch information
Showing
29 changed files
with
4,374 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"shp-kit": minor | ||
--- | ||
|
||
Implemented Polygon write and read |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
dist | ||
.DS_Store |
This file contains 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 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 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,112 @@ | ||
import { Feature } from "geojson"; | ||
import { ShapefileTypesString } from "../helpers/shapefileTypes"; | ||
import { Options } from "../../public/shpRead"; | ||
|
||
const getOptionalViewFloat64 = (view: DataView, target: number, lastValidByte: number, little: boolean) => { | ||
if (target > view.byteLength - 1 || target > lastValidByte) return 0; | ||
return view.getFloat64(target, little); | ||
}; | ||
|
||
const Polygon = ( | ||
shpView: DataView, | ||
o: Options, | ||
currByteIndex: number, | ||
recordLength: number, | ||
shpType: ShapefileTypesString, | ||
properties: { | ||
[key: string]: any; | ||
} | ||
) => { | ||
const recordEndByte = currByteIndex + recordLength; | ||
//significant header information | ||
const partsLength = shpView.getInt32(currByteIndex + 36, true); | ||
const pointsLength = shpView.getInt32(currByteIndex + 40, true); | ||
currByteIndex += 44; | ||
const partsSeparators = [...Array(partsLength)].map((_, i) => shpView.getInt32(currByteIndex + 4 * i, true)); | ||
partsSeparators.push(pointsLength); | ||
|
||
currByteIndex += 4 * partsLength; | ||
|
||
let points = [...Array(pointsLength)].map((_, i) => { | ||
return [shpView.getFloat64(currByteIndex + 16 * i, true), shpView.getFloat64(currByteIndex + 16 * i + 8, true)]; | ||
}); | ||
|
||
currByteIndex += 16 * pointsLength; | ||
|
||
let mValues: number[] | null = null; | ||
let zValues: number[] | null = null; | ||
if (shpType === "PolygonM") { | ||
currByteIndex += 32; // min-max | ||
mValues = [...Array(pointsLength)].map((_, i) => { | ||
return shpView.getFloat64(currByteIndex + 8 * i, true); | ||
}); | ||
} | ||
if (shpType === "PolygonZ") { | ||
currByteIndex += 16; // min-max | ||
zValues = [...Array(pointsLength)].map((_, i) => { | ||
return getOptionalViewFloat64(shpView, currByteIndex + 8 * i, recordEndByte, true); | ||
}); | ||
currByteIndex += pointsLength * 8 + 16; | ||
mValues = [...Array(pointsLength)].map((_, i) => { | ||
return getOptionalViewFloat64(shpView, currByteIndex + 8 * i, recordEndByte, true); | ||
}); | ||
|
||
if (!o.elevationPropertyKey) { | ||
points = points.map((pt, i) => [pt[0] as number, pt[1] as number, (zValues as number[])[i] as number]); | ||
} | ||
} | ||
|
||
const coordinates = partsSeparators.reduce( | ||
(list, v, i, a) => | ||
i === 0 ? list : [...list, [...Array(v - (a[i - 1] as number))].map((_, j) => points[(a[i - 1] as number) + j])], | ||
[] as any[] | ||
); | ||
|
||
const output = { | ||
type: "Feature", | ||
geometry: { | ||
type: "Polygon", | ||
coordinates, | ||
}, | ||
properties: { | ||
...properties, | ||
...(o.elevationPropertyKey && zValues | ||
? { | ||
[o.elevationPropertyKey]: | ||
zValues && [...new Set(zValues)].length === 1 | ||
? zValues[0] | ||
: partsSeparators.reduce( | ||
(list, v, i, a) => | ||
i === 0 | ||
? list | ||
: [ | ||
...list, | ||
[...Array(v - (a[i - 1] as number))].map((_, j) => zValues[(a[i - 1] as number) + j]), | ||
], | ||
[] as any[] | ||
), | ||
} | ||
: {}), | ||
...(o.measurePropertyKey && mValues | ||
? { | ||
[o.measurePropertyKey]: | ||
mValues && [...new Set(mValues)].length === 1 | ||
? mValues[0] | ||
: partsSeparators.reduce( | ||
(list, v, i, a) => | ||
i === 0 | ||
? list | ||
: [ | ||
...list, | ||
[...Array(v - (a[i - 1] as number))].map((_, j) => mValues[(a[i - 1] as number) + j]), | ||
], | ||
[] as any[] | ||
), | ||
} | ||
: {}), | ||
}, | ||
} as Feature; | ||
return output as Feature; | ||
}; | ||
|
||
export default Polygon; |
This file contains 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
Oops, something went wrong.