How to decompress compressedGeometry

4168
9
Jump to solution
10-10-2012 12:11 AM
Swee_HengPoon
New Contributor
[ATTACH=CONFIG]18312[/ATTACH]
Above shows a route with a javascript alertbox with the features of each of the segments of the routes. There is also a compressedGeometry in it. Is there any way to decode/decompress the compressedGeometry?
0 Kudos
1 Solution

Accepted Solutions
__Rich_
Occasional Contributor III
Primitive arguments in JavaScript are passed by value so in your code nIndex will always be zero, hence your infinite loop.

Are you getting this from a RouteTask?  You know that this is decompressed for you in the RouteResult etc?

View solution in original post

0 Kudos
9 Replies
__Rich_
Occasional Contributor III
Here's something in C# if you fancy porting it?

http://resources.arcgis.com/gallery/file/arcobjects-net-api/details?entryID=7B5F4B9F-1422-2418-A0AA-...

Looks like there is some code in the API for converting compressed geometry to points and then to a polyline, but it's not 'public', it'd be up to ESRI if they want to share it.
0 Kudos
Swee_HengPoon
New Contributor
Is there any sources in javascript? I need it in javascript as all my codes are in javascript and html. I am not using C#.
0 Kudos
__Rich_
Occasional Contributor III
I need it in javascript as all my codes are in javascript and html. I am not using C#.


That why I said you'd have to port it, i.e. from C# to JavaScript.

Have you looked at the code?  It's not that complicated.
0 Kudos
Swee_HengPoon
New Contributor
That why I said you'd have to port it, i.e. from C# to JavaScript.

Have you looked at the code?  It's not that complicated.


Ya I have seen the codes inside the file. They have something called the Path inside. What do I change the Path to? Polyline?
0 Kudos
__Rich_
Occasional Contributor III
Ya I have seen the codes inside the file. They have something called the Path inside. What do I change the Path to? Polyline?


Maybe....you could do a little research, find out what a "Path" (in that context) is and then relate it to the classes available in the JS API.

Here's somewhere to start:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/002m/002m0000045n000000.htm

Which might lead you to:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/polyline.htm#addPath
0 Kudos
Swee_HengPoon
New Contributor
Maybe....you could do a little research, find out what a "Path" (in that context) is and then relate it to the classes available in the JS API.

Here's somewhere to start:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/002m/002m0000045n000000.htm

Which might lead you to:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/polyline.htm#addPath


Hi can you please help me check if there is anything wrong with my codes?It keep looping on stage 3 and 4 non-stop. Below is the code snippets that I have converted in javascript.

  function check(){
   alert("stage 1");
   //alert("\nCompressed geometry with no Ms, no Zs:");
   //PrintPath(CreatePathFromCompressedGeometry("+1m91-6fkfr+202tp+k+f+7+3+34+2d"));

   //alert("\nCompressed geometry with both Ms, Zs:");
   //PrintPath(CreatePathFromCompressedGeometry("+0+1+3+1+emjd+3j07m+3+0+0+1-3-1|+9og+0+lv4+0+lv4|+5rg+uq+r9+au+168"));

   PrintPath(CreatePathFromCompressedGeometry("+1+hrt+139j+0+0"));
  }

  function PrintPath(pth){
   alert("stage 5");
   //alert(pth);
   for (i = 0; i < pth.length; i++){
    var p = pth;
    alert(p);
    
    //PointN p = pth.PointArray as PointN;
    //alert(p.x + ",\t" + p.y);
   }
  }

  function CreatePathFromCompressedGeometry(compresedGeometry){
   alert("stage 2");
   var points = [];

   var nIndex = 0;
         var dMultBy = parseFloat(ExtractInt(compresedGeometry, nIndex)); // exception
   var nLastDiffX = 0;
   var nLastDiffY = 0;
   var nLength = compresedGeometry.length; // reduce call stack

   while (nIndex != nLength) {
    // extract number
           var nDiffX = ExtractInt(compresedGeometry, nIndex); // exception
           var nDiffY = ExtractInt(compresedGeometry, nIndex); // exception

           // decompress
           var nX = nDiffX + nLastDiffX;
           var nY = nDiffY + nLastDiffY;
           var dX = parseFloat(nX / dMultBy);
           var dY = parseFloat(nY / dMultBy);
           // add result item
           var point = new esri.geometry.Point(dX, dY, new esri.SpatialReference({ wkid: 3414 }));
           //point.x = dX;
           //point.y = dY;
           points.push(point); // memory exception
           // prepare for next calculation
           nLastDiffX = nX;
           nLastDiffY = nY;
   }
   return points;
  }

  function ExtractInt(src, nStartPos) {
   alert("stage 3");
   // Read one integer from compressed geometry string by using passed position
   // Returns extracted integer, and re-writes nStartPos for the next integer
   var bStop = false;
   var result = "";
   var nCurrentPos = nStartPos;
   while (!bStop) {
    var cCurrent = src[nCurrentPos];
           if (cCurrent == '+' || cCurrent == '-' || cCurrent == '|') {
     if (nCurrentPos != nStartPos) {
      bStop = true;
      continue;
     }
    }
    result += cCurrent;
    nCurrentPos++;
    if (nCurrentPos == src.length) // check overflow
     bStop = true;
   }
         var nResult = 0;
         if (result.length != 0) {
           nResult = FromStringRadix32(result.toString());
           nStartPos = nCurrentPos;
         }
         return nResult;
  }

  function FromStringRadix32(s) {
   alert("stage 4");
   // Sample input and output: +1lmo -> 55000
   var result = 0;
   for (i = 1; i < s.length; i++) {
    var cur = s;
    //Assert.IsTrue((cur >= '0' && cur <= '9') || (cur >= 'a' && cur <= 'v'), "Cannot parse CompressedGeometry");
    if (cur >= '0' && cur <= '9')
              result = (result << 5) + parseInt(cur) - parseInt('0');
           else if (cur >= 'a' && cur <= 'v')
              result = (result << 5) + parseInt(cur) - parseInt('a') + 10;
           //else Assert.Fail("Cannot parse CompressedGeometry");
         }
         if (s[0] == '-')
           result = -result;
   return result;
  }
0 Kudos
__Rich_
Occasional Contributor III
Primitive arguments in JavaScript are passed by value so in your code nIndex will always be zero, hence your infinite loop.

Are you getting this from a RouteTask?  You know that this is decompressed for you in the RouteResult etc?
0 Kudos
Swee_HengPoon
New Contributor
Primitive arguments in JavaScript are passed by value so in your code nIndex will always be zero, hence your infinite loop.

Are you getting this from a RouteTask?  You know that this is decompressed for you in the RouteResult etc?


Hi, thanks for your help. I managed to solve it.
0 Kudos
CoreyAlix1
New Contributor II

Do you want to share your javascript port?  Maybe on gist.github.com?