-
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
base: master
Are you sure you want to change the base?
Changes from all commits
edec203
f13c83d
6b8a86e
cc934a6
de2c00b
10a3048
e73e9e5
bc7ab31
2dc58c4
7290f62
855dc13
d87ce6d
f29b0eb
5dbd050
873b67f
c87f633
f39e599
c260796
27f0d79
e97757d
5c85e68
64d7c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 default 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do these need commas or semicolons at the end of the lines? |
||
| /** 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[][]>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it returns a 3D array like [band][row][column], so should be |
||
| /** experimental! returns a canvas picture of the data. */ | ||
| toCanvas: (options: { height?: number; width?: number }) => ImageData | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function basically calls https://github.com/GeoTIFF/georaster-to-canvas/blob/master/index.js. It returns the canvas created by |
||
| } | ||
|
|
||
| export type GeorasterMetadata = Pick< | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't used |
||
| Georaster, | ||
| | "noDataValue" | ||
| | "projection" | ||
| | "xmin" | ||
| | "ymax" | ||
| | "pixelWidth" | ||
| | "pixelHeight" | ||
| >; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Internal test of Typescript types. Imports from src not dist. | ||
| // downstream TS users of geotiff should simply import the geotiff library and will get types along with the dist build | ||
|
|
||
| import { assert } from "console"; | ||
| import parseGeoraster from "../src"; | ||
| 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 | ||
| }; | ||
| if (!georaster.getValues) throw new Error('georaster configured without URL') | ||
| 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); | ||
| } | ||
| }); |
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.
errant commit, this needs to be removed before merging