-
Notifications
You must be signed in to change notification settings - Fork 37
Add Typescript definitions #59
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
twelch
wants to merge
22
commits into
GeoTIFF:master
Choose a base branch
from
twelch:ts-types
base: master
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 16 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
edec203
initial ts declaration file with build step
twelch f13c83d
add types to package.json
twelch 6b8a86e
add module declaration
twelch cc934a6
add comments, drop declaration
twelch de2c00b
newline
twelch 10a3048
restructure with default export and namespace for additional type exp…
twelch e73e9e5
solve default export + additional
twelch bc7ab31
nit
twelch 2dc58c4
projection is always a number
twelch 7290f62
Fix y ordering. Add getValues and toCanvas
twelch 855dc13
make getValues optionally available
twelch d87ce6d
corrections
twelch f29b0eb
loosen resampleMethod type to reduce friction with use
twelch 5dbd050
improve jsdoc
twelch 873b67f
Switch from number to TypedArray
twelch c87f633
add TS type testing
twelch f39e599
change declaration file name to fix test import
twelch c260796
clean up comments
twelch 27f0d79
change built type dec file to match library name, index is used inter…
twelch e97757d
switch back to top-level import, use esm default export style
twelch 5c85e68
switch ts test to internal import
twelch 64d7c64
no npx
twelch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /** Typed array of data values, the basic building block of a georaster */ | ||
| type TypedArray = | ||
| | number[] | ||
| | Uint8Array | ||
| | Int8Array | ||
| | Uint16Array | ||
| | Int16Array | ||
| | Uint32Array | ||
| | Int32Array | ||
| | Float32Array | ||
| | Float64Array; | ||
|
|
||
| declare function parseGeoraster( | ||
| /** raster pixel data, accepts variety of forms */ | ||
| data: object | string | Buffer | ArrayBuffer | TypedArray[][], | ||
| /** raster metadata */ | ||
| metadata?: parseGeoraster.GeorasterMetadata, | ||
| /** whether or not to print debug statements */ | ||
| debug?: boolean | ||
| ): Promise<parseGeoraster.Georaster>; | ||
|
|
||
| // Match default CJS export in index.js | ||
| export = parseGeoraster; | ||
|
|
||
| // A namespace with the same name as the default export is needed to define additional type exports | ||
| // https://stackoverflow.com/a/51238234/4159809 | ||
| declare namespace parseGeoraster { | ||
| /** defines the new raster image to generate as a window in the source raster image. Resolution (cell size) is determined from this */ | ||
| export interface WindowOptions { | ||
| /** left side of the image window in pixel coordinates */ | ||
| left: number | ||
| /** top of the image window in pixel coordinates */ | ||
| top: number | ||
| /** right of the image window in pixel coordinates. Should be greater than left */ | ||
| right: number | ||
| /** bottom of the image window in pixel coordinates. Should be greater than top */ | ||
| bottom: number | ||
| /** width in pixels to make the resulting raster. Will resample and/or use overview if not same as right - left */ | ||
| width: number | ||
| /** height in pixels to make the resulting raster. Will resample and/or use overview if not same as bottom - top */ | ||
| height: number | ||
| /** method to map src raster values to result raster. Supports 'nearest' neighbor, defaults to 'bilinear' */ | ||
| resampleMethod?: string | ||
| } | ||
|
|
||
| export interface Georaster { | ||
| /** raster values for one or more bands. Represented as [band, column, row] */ | ||
| values: TypedArray[][]; | ||
| /** raster height in units of projection */ | ||
| height: number; | ||
| /** raster width in units of projection */ | ||
| width: number; | ||
| /** raster height in pixels */ | ||
| pixelHeight: number; | ||
| /** raster width in pixels */ | ||
| pixelWidth: number; | ||
| /** Projection identifier */ | ||
| projection: number; | ||
| /** left boundary, in units of projection*/ | ||
| xmin: number; | ||
| /** right boundary, in units of projection */ | ||
| xmax: number; | ||
| /** bottom boundary, in units of projection */ | ||
| ymin: number; | ||
| /** top boundary, in units of projection */ | ||
| ymax: number; | ||
| /** cell value representing "no data" in raster */ | ||
| noDataValue: number; | ||
| /** number of raster bands */ | ||
| numberOfRasters: number; | ||
| /** Minimum cell value for each raster band. Indexed by band number */ | ||
| mins: number[]; | ||
| /** Maximum cell value for each raster band. Indexed by band number */ | ||
| maxs: number[]; | ||
| /** difference between max and min for each raster band. Indexed by band number */ | ||
| ranges: number[]; | ||
| /** if raster initialized with a URL, this method is available to fetch a | ||
| * specific subset or 'window' without reading the entire raster into memory. | ||
| * If the window options do not align exactly with the source image then a new | ||
| * one is generated using the resampleMethod. The best available overview will | ||
| * also be used if they are available. */ | ||
| getValues?: (options: WindowOptions) => Promise<TypedArray[][]>; | ||
| /** experimental! returns a canvas picture of the data. */ | ||
| toCanvas: (options: { height?: number; width?: number }) => ImageData | ||
| } | ||
|
|
||
| export type GeorasterMetadata = Pick< | ||
| Georaster, | ||
| | "noDataValue" | ||
| | "projection" | ||
| | "xmin" | ||
| | "ymax" | ||
| | "pixelWidth" | ||
| | "pixelHeight" | ||
| >; | ||
| } |
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,81 @@ | ||
| // Tests use of Typescript types, requires build to already be done into dist | ||
|
|
||
| import { assert } from "console"; | ||
| import parseGeoraster from "../../georaster"; // Import from outside to utilize dist and types pointer in package.json | ||
| import { countIn2D } from "../src/utils"; | ||
|
|
||
| // Floating point number values | ||
|
|
||
| const values = [ | ||
| [ | ||
| [0, 1, 2], | ||
| [0, 0, 0], | ||
| [2, 1, 1] | ||
| ] | ||
| ]; | ||
|
|
||
| const noDataValue = 3; | ||
| const projection = 4326; | ||
| const xmin = 10; // left | ||
| const ymax = 13; // top | ||
| const pixelWidth = 1; | ||
| const pixelHeight = 1; | ||
| const metadata = { | ||
| noDataValue, | ||
| projection, | ||
| xmin, | ||
| ymax, | ||
| pixelWidth, | ||
| pixelHeight, | ||
| }; | ||
|
|
||
| parseGeoraster(values, metadata).then(georaster => { | ||
| const values = georaster.values | ||
| console.log('number raster values', values) | ||
| assert(values.length === 1) // single band | ||
| assert(values[0].length === 3) | ||
| values[0].forEach(row => assert(Array.isArray(row))) // Should be standard javascript Array type | ||
| assert(values[0][0][2] === 2) | ||
| }); | ||
|
|
||
| //// Unsigned 8-bit integer values | ||
|
|
||
| const unsignedValues = values.map(band => | ||
| band.map(row => new Uint8Array(row)) | ||
| ); | ||
|
|
||
| parseGeoraster(unsignedValues, metadata).then(georaster => { | ||
| const values = georaster.values | ||
| console.log('unsigned 8-bit int raster values', values) | ||
| assert(values.length === 1) // single band | ||
| assert(values[0].length === 3) | ||
| values[0].forEach(row => assert(typeof row === 'object')) // Typed arrays in Javascript are not of Array type | ||
| assert(values[0][0][2] === 2) // But they do behave like arrays for read access and return numbers | ||
| }); | ||
|
|
||
| /// COG test | ||
|
|
||
| const raster_url = "https://landsat-pds.s3.amazonaws.com/c1/L8/024/030/LC08_L1TP_024030_20180723_20180731_01_T1/LC08_L1TP_024030_20180723_20180731_01_T1_B1.TIF"; | ||
| parseGeoraster(raster_url).then(georaster => { | ||
| try { | ||
| const options = { | ||
| left: 0, | ||
| top: 0, | ||
| right: 4000, | ||
| bottom: 4000, | ||
| width: 10, | ||
| height: 10 | ||
| }; | ||
| georaster.getValues(options).then(values => { | ||
| const numBands = values.length; | ||
| const numRows = values[0].length; | ||
| const numColumns = values[0][0].length; | ||
|
|
||
| // checking histogram for first and only band | ||
| const histogram = countIn2D(values[0]); | ||
| assert(histogram[0] === 39) | ||
| }); | ||
| } catch (error) { | ||
| console.error('error:', error); | ||
| } | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.