Skip to content

Instantly share code, notes, and snippets.

@ca0v
Last active January 4, 2016 18:38
Show Gist options
  • Save ca0v/6a09c2fe6dfdfc0f5192 to your computer and use it in GitHub Desktop.
Save ca0v/6a09c2fe6dfdfc0f5192 to your computer and use it in GitHub Desktop.
Port of C# code published by ESRI
/*************************************************
* see http://resources.esri.com/help/9.3/arcgisengine/ArcObjects/esrinetworkanalyst/INACompactStreetDirection_CompressedGeometry.htm
*************************************************/
class DecompressGeometry {
public ExtractPointsFromCompressedGeometry(compresedGeometry: string)
{
// initialize result storage
let result = <Array<{x: number; y: number;}>>[],
nIndex = {ref: 0},
dMultBy = this.ExtractInt(compresedGeometry, nIndex),
nLastDiffX = 0,
nLastDiffY = 0,
nLength = compresedGeometry.length;
while (nIndex.ref != nLength)
{
// extract number
let nDiffX = this.ExtractInt(compresedGeometry, nIndex);
let nDiffY = this.ExtractInt(compresedGeometry, nIndex);
// decompress
let nX = nDiffX + nLastDiffX;
let nY = nDiffY + nLastDiffY;
let dX = nX / dMultBy;
let dY = nY / dMultBy;
// add result item
let point = {x : dX, y: dY};
result.push(point);
// prepare for next calculation
nLastDiffX = nX;
nLastDiffY = nY;
}
// return result
return result;
}
// Read one integer from compressed geometry string by using passed position
// Returns extracted integer, and re-writes nStartPos for the next integer
private ExtractInt(src: string, nStartPos: {ref: number})
{
let bStop = false,
result = "",
nCurrentPos = nStartPos.ref;
while (!bStop)
{
let cCurrent = src[nCurrentPos];
if (cCurrent == '+' || cCurrent == '-')
{
if (nCurrentPos != nStartPos.ref)
{
bStop = true;
continue;
}
}
result += cCurrent;
nCurrentPos++;
if (nCurrentPos == src.length) // check overflow
bStop = true;
}
let nResult = Number.MIN_VALUE;
if (result.length != 0)
{
nResult = parseInt(result, 32);
nStartPos.ref = nCurrentPos;
}
return nResult;
}
}
@ca0v
Copy link
Author

ca0v commented Jan 4, 2016

Usage Example:

let decomp = new DecompressGeometry();
console.log(decomp.ExtractPointsFromCompressedGeometry("+1m91-66mmp+1nfmv+0+0"));

@ca0v
Copy link
Author

ca0v commented Jan 4, 2016

A much better version can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment