Comparing version 0.0.0 to 0.1.0
@@ -1,3 +0,4 @@ | ||
const isAuxXML = require("is-aux-xml"); | ||
const readAuxXML = require("read-aux-xml"); | ||
const isAuxXML = require("./is-aux-xml.js"); | ||
const readAuxXML = require("./read-aux-xml.js"); | ||
const writeAuxXML = require("./write-aux-xml.js"); | ||
@@ -7,2 +8,3 @@ module.exports = { | ||
readAuxXML, | ||
writeAuxXML | ||
}; |
const getByte = require("get-byte"); | ||
module.exports = function (data, debug) { | ||
module.exports = function isAuxXML(data, debug) { | ||
if (debug) console.log("data:", data); | ||
const length = | ||
data.length !== undefined | ||
? data.length | ||
: data.byteLength !== undefined | ||
? data.byteLength | ||
: null; | ||
const length = data.length !== undefined ? data.length : data.byteLength !== undefined ? data.byteLength : null; | ||
if (debug) console.log("length:", length); | ||
@@ -16,5 +11,3 @@ | ||
const str = data.trim().toLowerCase(); | ||
return ( | ||
length > 4 && (str.startsWith("<pamdataset") || str.endsWith(".aux.xml")) | ||
); | ||
return length > 4 && (str.startsWith("<pamdataset") || str.endsWith(".aux.xml")); | ||
} else { | ||
@@ -21,0 +14,0 @@ if (length < 8) { |
{ | ||
"name": "aux-xml", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Identify and Read Auxiliary XML Files (e.g., .jpg.aux.xml, .png.aux.xml, and .tif.aux.xml)", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"is-aux-xml.js", | ||
"read-aux-xml.js", | ||
"write-aux-xml.js" | ||
], | ||
"scripts": { | ||
"format": "npx prettier --write *.js", | ||
"test": "ava --verbose" | ||
"format": "npx prettier --arrow-parens=avoid --print-width=160 --trailing-comma=none --write *.js", | ||
"test": "node test.js" | ||
}, | ||
@@ -31,8 +37,9 @@ "repository": { | ||
"dependencies": { | ||
"get-byte": "0.0.0", | ||
"xml-utils": "^0.2.0" | ||
"camelcase": "^6.2.0", | ||
"get-byte": "^0.0.0", | ||
"xml-utils": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.12.1" | ||
"flug": "^2.1.0" | ||
} | ||
} |
@@ -1,10 +0,12 @@ | ||
const findTagByPath = require("xml-utils/src/find-tag-by-path"); | ||
const findTagsByName = require("xml-utils/src/find-tags-by-name"); | ||
const getAttribute = require("xml-utils/src/get-attribute"); | ||
const camelcase = require("camelcase"); | ||
const findTagByName = require("xml-utils/find-tag-by-name"); | ||
const findTagByPath = require("xml-utils/find-tag-by-path"); | ||
const findTagsByName = require("xml-utils/find-tags-by-name"); | ||
const getAttribute = require("xml-utils/get-attribute"); | ||
module.exports = (xml, { debug = false } = { debug: false }) => { | ||
module.exports = function readAuxXML (xml, { debug = false } = { debug: false }) { | ||
if (debug) console.log("[aux-xml] starting readAuxXML"); | ||
if (typeof Buffer !== undefined && Buffer.isBuffer(xml)) { | ||
xml = Buffer.toString(); | ||
xml = xml.toString(); | ||
} else if (xml instanceof ArrayBuffer) { | ||
@@ -23,17 +25,69 @@ xml = String.fromCharCode(...new Uint8Array(xml)); | ||
const metadatas = findTagsByName(xml, "Metadata"); | ||
const mdis = findTagsByName(xml, "MDI"); | ||
if (debug) console.log("[aux-xml] mdis:", mdis); | ||
const interleave = mdis.find( | ||
(mdi) => getAttribute(mdi, "key") === "INTERLEAVE" | ||
); | ||
if (interleave) result.interleave = interleave.inner.toLowerCase(); | ||
const interleave = mdis.find(mdi => getAttribute(mdi, "key").toUpperCase() === "INTERLEAVE"); | ||
if (interleave) result.interleave = interleave.inner; | ||
const pyramidResamplingType = mdis.find( | ||
(mdi) => getAttribute(mdi, "key") === "PyramidResamplingType" | ||
); | ||
if (pyramidResamplingType) | ||
result.pyramidResamplingType = pyramidResamplingType.inner.toLowerCase(); | ||
const sourceColorSpace = mdis.find(mdi => getAttribute(mdi, "key").toUpperCase() === "SOURCE_COLOR_SPACE"); | ||
if (sourceColorSpace) result.sourceColorSpace = sourceColorSpace.inner; | ||
const pyramidResamplingType = mdis.find(mdi => getAttribute(mdi, "key") === "PyramidResamplingType"); | ||
if (pyramidResamplingType) result.pyramidResamplingType = pyramidResamplingType.inner; | ||
const main = metadatas.find(tag => tag.inner.toLowerCase().includes("interleave")); | ||
// findTags(xml, { name: "MDI",}) | ||
const compression = findTagsByName(main.inner, "MDI")?.find(tag => getAttribute(tag.outer, "key").toUpperCase() === "COMPRESSION"); | ||
if (compression) result.compression = compression.inner; | ||
const exifs = mdis | ||
.filter(mdi => getAttribute(mdi, "key").toUpperCase().startsWith("EXIF")) | ||
.map(mdi => [getAttribute(mdi, "key").replace(/EXIF_/, ""), mdi.inner]) | ||
.reduce((obj, [key, value]) => (obj[camelcase(key)] = value) && obj, {}); | ||
if (Object.keys(exifs).length > 0) result.exifs = exifs; | ||
const bands = findTagsByName(xml, "PAMRasterBand") | ||
.map(({ inner, outer }) => { | ||
const band = { | ||
band: getAttribute(outer, "band") | ||
}; | ||
const mdisBand = findTagsByName(inner, "MDI"); | ||
const compression = mdisBand.find(tag => getAttribute(tag.outer, "key").toUpperCase() === "COMPRESSION"); | ||
if (compression) band.compression = compression.inner; | ||
const stats = mdisBand | ||
.filter(mdi => getAttribute(mdi, "key").toUpperCase().startsWith("STATISTICS")) | ||
.map(mdi => [getAttribute(mdi, "key").replace(/STATISTICS_/, ""), mdi.inner]) | ||
.reduce((obj, [key, value]) => (obj[camelcase(key)] = value) && obj, {}); | ||
if (Object.keys(stats).length > 0) band.stats = stats; | ||
const histItem = findTagByName(inner, "HistItem"); | ||
if (histItem) { | ||
const histogram = {}; | ||
["HistMin", "HistMax", "BucketCount", "IncludeOutOfRange", "Approximate"].forEach(name => { | ||
const tag = findTagByName(histItem.inner, name); | ||
if (tag) histogram[camelcase(name)] = tag.inner; | ||
}); | ||
const histCounts = findTagByName(histItem.inner, "HistCounts"); | ||
if (histCounts) histogram.histCounts = histCounts.inner.split("|"); | ||
if (Object.keys(histogram).length > 0) band.histogram = histogram; | ||
} | ||
return band; | ||
}) | ||
.reduce((obj, band) => (obj[band.band] = band) && obj, {}); | ||
if (Object.keys(bands).length > 0) { | ||
result.bands = bands; | ||
} | ||
return result; | ||
}; |
@@ -13,15 +13,22 @@ :warning: This project is a work in progress. | ||
# identify aux.xml files | ||
```javascript | ||
```js | ||
const fs = require("fs"); | ||
const isAuxXML = require("aux-xml/is-aux-xml"); | ||
const file = readFileSync("test.aux.xml"); | ||
isAuxXml(file); | ||
isAuxXML("test.aux.xml"); | ||
// true | ||
const text = readFileSync("test.aux.xml", "utf-8"); | ||
isAuxXML(text); | ||
// true | ||
const buffer = readFileSync("test.aux.xml"); | ||
isAuxXML(buffer); | ||
// true | ||
``` | ||
# read aux.xml files | ||
```javascript | ||
```js | ||
const fs = require("fs"); | ||
const isAuxXML = require("aux-xml/is-aux-xml"); | ||
const readAuxXML = require("aux-xml/read-aux-xml"); | ||
@@ -37,2 +44,24 @@ const file = readFileSync("test.aux.xml"); | ||
*/ | ||
``` | ||
``` | ||
For larger example of sample output of readAuxXML see [flower.jpg.aux.xml.json](https://github.com/DanielJDufour/aux-xml/blob/main/data/flower.jpg.aux.xml.json). | ||
# write aux.xml files | ||
```js | ||
const fs = require("fs"); | ||
const writeAuxXML = require("aux-xml/write-aux-xml"); | ||
const file = readFileSync("test.aux.xml"); | ||
const text = writeAuxXML({ | ||
srs: 'PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]', | ||
interleave: 'pixel' | ||
}); | ||
``` | ||
text will be | ||
```js | ||
`<PAMDataset> | ||
<SRS>PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]</SRS> | ||
<Metadata domain="IMAGE_STRUCTURE"> | ||
<MDI key="INTERLEAVE">PIXEL</MDI> | ||
</Metadata> | ||
</PAMDataset>` | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
147
66
0
17231
3
7
+ Addedcamelcase@^6.2.0
+ Addedcamelcase@6.3.0(transitive)
+ Addedxml-utils@1.10.1(transitive)
- Removedxml-utils@0.2.0(transitive)
Updatedget-byte@^0.0.0
Updatedxml-utils@^1.0.2