@sean_kenny/coloured-bitmap-to-geojson-polygons-js
Advanced tools
Comparing version 0.2.0 to 0.3.0
import { BmpFileMetadata } from "./bmp-file-metadata.js"; | ||
import { OutputGeoJSON } from "./geojson-types.js"; | ||
import { DomainBounds } from "./domain-bounds.js"; | ||
export interface ExportColouredBitmapToGeoJSONPolygonsInput<TData extends Record<string, unknown>> { | ||
inputFilePath: string; | ||
colourToPropertiesMap: Record<string, TData>; | ||
domainBounds?: DomainBounds | undefined; | ||
} | ||
@@ -7,0 +9,0 @@ export interface ExportColouredBitmapToGeoJSONPolygonsOutput<TData extends Record<string, unknown>> { |
@@ -7,2 +7,8 @@ import { readFile } from "fs/promises"; | ||
const bmpData = await readFile(input.inputFilePath); | ||
const domainBounds = input.domainBounds ?? { | ||
latitudeLowerBound: -90, | ||
latitudeUpperBound: 90, | ||
longitudeLowerBound: -180, | ||
longitudeUpperBound: 180 | ||
}; | ||
const hexBmpData = bmpData.toString('hex'); | ||
@@ -32,3 +38,4 @@ const { header, headerSizeBytes } = extractHeaderFromHexBmpData({ hexBmpData }); | ||
allColoursPresent, | ||
colourToPropertiesMap: input.colourToPropertiesMap | ||
colourToPropertiesMap: input.colourToPropertiesMap, | ||
domainBounds | ||
}); | ||
@@ -35,0 +42,0 @@ return { |
@@ -0,1 +1,2 @@ | ||
import { DomainBounds } from "../domain-bounds.js"; | ||
import { Feature } from "../geojson-types.js"; | ||
@@ -10,2 +11,3 @@ interface ConvertCornerIndexPolygonIntoGeoJSONFeatureInput<TData extends Record<string, unknown>> { | ||
bitmapHeightPx: number; | ||
domainBounds: DomainBounds; | ||
} | ||
@@ -12,0 +14,0 @@ interface ConvertCornerIndexPolygonIntoGeoJSONFeatureOutput<TData extends Record<string, unknown>> { |
const rgbToHexString = (input) => `#${input.red.toString(16).padStart(2, '0')}${input.green.toString(16).padStart(2, '0')}${input.blue.toString(16).padStart(2, '0')}`; | ||
export const convertCornerIndexPolygonIntoGeoJSONFeature = (input) => { | ||
const colourHexString = rgbToHexString({ | ||
red: input.red, | ||
green: input.green, | ||
blue: input.blue | ||
}).toLowerCase(); | ||
const { cornersForPolygon, domainBounds, colourToPropertiesMap, red, blue, green, bitmapWidthPx, bitmapHeightPx } = input; | ||
const { latitudeLowerBound, latitudeUpperBound, longitudeLowerBound, longitudeUpperBound } = domainBounds; | ||
const colourHexString = rgbToHexString({ red, green, blue }).toLowerCase(); | ||
return { | ||
@@ -14,5 +12,5 @@ feature: { | ||
coordinates: [ | ||
input.cornersForPolygon.map(([cornerColIndex, cornerRowIndex]) => [ | ||
-180 + ((cornerColIndex / input.bitmapWidthPx) * 360), | ||
90 - ((cornerRowIndex / input.bitmapHeightPx) * 180), | ||
cornersForPolygon.map(([cornerColIndex, cornerRowIndex]) => [ | ||
longitudeLowerBound + ((cornerColIndex / bitmapWidthPx) * (longitudeUpperBound - longitudeLowerBound)), | ||
latitudeUpperBound - ((cornerRowIndex / bitmapHeightPx) * (latitudeUpperBound - latitudeLowerBound)), | ||
]) | ||
@@ -23,3 +21,3 @@ ], | ||
colourHexCode: colourHexString, | ||
data: input.colourToPropertiesMap[colourHexString] ?? null | ||
data: colourToPropertiesMap[colourHexString] ?? null | ||
} | ||
@@ -26,0 +24,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { DomainBounds } from "../domain-bounds.js"; | ||
import { Feature } from "../geojson-types.js"; | ||
@@ -16,2 +17,3 @@ interface ConsolidateImageDataIntoFeaturesInput<TData extends Record<string, unknown>> { | ||
bitmapHeightPx: number; | ||
domainBounds: DomainBounds; | ||
} | ||
@@ -18,0 +20,0 @@ interface ConsolidateImageDataIntoFeaturesOutput<TData extends Record<string, unknown>> { |
@@ -5,3 +5,3 @@ import { consolidatePolygonCellsIntoCornerIndexPolygonInput } from "./consolidate-polygon-cells-into-corner-index-polygon.js"; | ||
export const consolidateImageDataIntoFeatures = (input) => { | ||
const { imageData, bitmapWidthPx, bitmapHeightPx, colourToPropertiesMap } = input; | ||
const { imageData, bitmapWidthPx, bitmapHeightPx, colourToPropertiesMap, domainBounds } = input; | ||
const { polygonCellCollection } = segmentDataIntoPolygonCellCollection({ | ||
@@ -20,3 +20,4 @@ imageData | ||
bitmapWidthPx, | ||
bitmapHeightPx | ||
bitmapHeightPx, | ||
domainBounds | ||
}).feature) | ||
@@ -23,0 +24,0 @@ .filter((element) => !!element); |
{ | ||
"name": "@sean_kenny/coloured-bitmap-to-geojson-polygons-js", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A library for convering colour segmented bitmaps to a GeoJSON file of polygons.", | ||
@@ -20,4 +20,4 @@ "scripts": { | ||
"@types/node": "^20.14.10", | ||
"typescript": "^5.5.4", | ||
"vitest": "^2.0.5" | ||
"typescript": "^5.6.2", | ||
"vitest": "^2.1.1" | ||
}, | ||
@@ -24,0 +24,0 @@ "engines": { |
@@ -115,3 +115,11 @@ # coloured-bitmap-to-geojson-js | ||
### domainBounds | ||
`domainBounds` is an additional optional argument to `exportColouredBitmapToGeoJSONPolygons` which allows you to specify where the corners of your output GeoJSON file should be on the globe. The default `domainBounds` values is `{ latitudeLowerBound: -90, latitudeUpperBound: 90, longitudeLowerBound: -180, longitudeUpperBound: 180 }` which covers the entire globe. The image below shows the effect of a `domainBounds` value of `{ domainLatitudeLowerBound: -30, domainLatitudeUpperBound: 60, domainLongitudeLowerBound: -20, domainLongitudeUpperBound: 20}` on a `.bmp` file. | ||
Original BMP file | Output GeoJSON file | ||
:-------------------------:|:-------------------------: | ||
![](https://github.com/SeanKennyNF/coloured-bitmap-to-geojson-polygons-js/blob/main/readme-images/domain-bounds-example.png) | ![](https://github.com/SeanKennyNF/coloured-bitmap-to-geojson-polygons-js/blob/main/readme-images/domain-bounds-example-output.png) | ||
## Some sample inputs and outputs | ||
@@ -118,0 +126,0 @@ |
79791
46
1679
137