简体   繁体   中英

Polygon compression for Integer coordinates

I have polygon in raster with "signed int" 2D coordinates. Polygon has its vertices ordered in clockwise direction.

I want to store this polygon in some more "space friendly" way, that just store the sequence of int x, y coordinates. If I compress this file with rar, I got sizes about 3.5x smaller than uncompressed. Is there any better representation than simply storing x,y in a sequence?

The compression should be lossless.

If your polygon is (x1, y1), (x2, y2) ... (xn, yn), you can write it as,

x1, x2-x1, x3-x2, xn-xn-1, y1, y2-y1 ..., yn-yn-1

Typically the differences between adjacent x,y values will be smaller than the absolute values, so you can store the deltas as variable length ints . By doing this, each x,y pair can typically be stored in 2 or 4 bytes rather than 8.

It's hard to provide a definite answer as the result depends on a lot of factors. I did a fast test with GZIPOutputStream to compress individual polygons and the results depend much on the number of points of the polygon, but also on its area.

Basically I created polygons with an increasing number of vertices randomly distributed in a predefined area. Then I sorted the vertices clockwise and compressed the differences with GZIP

For polygons that fit into a standard screen (1440x900):

D Npoints: 3 Uncompressed: 24 Compressed: 41 Ratio: 0.58536583
D Npoints: 4 Uncompressed: 32 Compressed: 49 Ratio: 0.6530612
D Npoints: 5 Uncompressed: 40 Compressed: 58 Ratio: 0.6896552
D Npoints: 6 Uncompressed: 48 Compressed: 61 Ratio: 0.78688526
D Npoints: 7 Uncompressed: 56 Compressed: 66 Ratio: 0.8484849
D Npoints: 8 Uncompressed: 64 Compressed: 72 Ratio: 0.8888889
D Npoints: 9 Uncompressed: 72 Compressed: 80 Ratio: 0.9
D Npoints: 10 Uncompressed: 80 Compressed: 84 Ratio: 0.95238096
D Npoints: 11 Uncompressed: 88 Compressed: 89 Ratio: 0.98876405
D Npoints: 12 Uncompressed: 96 Compressed: 94 Ratio: 1.0212766
D Npoints: 13 Uncompressed: 104 Compressed: 96 Ratio: 1.0833334
D Npoints: 14 Uncompressed: 112 Compressed: 102 Ratio: 1.0980393
D Npoints: 15 Uncompressed: 120 Compressed: 108 Ratio: 1.1111112
. . .
D Npoints: 99 Uncompressed: 792 Compressed: 457 Ratio: 1.7330415

The compression ratio decreases for larger areas (1440*4x900*4):

D Npoints: 3 Uncompressed: 24 Compressed: 45 Ratio: 0.53333336
D Npoints: 4 Uncompressed: 32 Compressed: 55 Ratio: 0.58181816
D Npoints: 5 Uncompressed: 40 Compressed: 60 Ratio: 0.6666667
D Npoints: 6 Uncompressed: 48 Compressed: 67 Ratio: 0.7164179
D Npoints: 7 Uncompressed: 56 Compressed: 70 Ratio: 0.8
D Npoints: 8 Uncompressed: 64 Compressed: 79 Ratio: 0.8101266
D Npoints: 9 Uncompressed: 72 Compressed: 87 Ratio: 0.82758623
D Npoints: 10 Uncompressed: 80 Compressed: 93 Ratio: 0.86021507
D Npoints: 11 Uncompressed: 88 Compressed: 102 Ratio: 0.8627451
D Npoints: 12 Uncompressed: 96 Compressed: 109 Ratio: 0.88073397
D Npoints: 13 Uncompressed: 104 Compressed: 113 Ratio: 0.920354
D Npoints: 14 Uncompressed: 112 Compressed: 119 Ratio: 0.9411765
D Npoints: 15 Uncompressed: 120 Compressed: 125 Ratio: 0.96
D Npoints: 16 Uncompressed: 128 Compressed: 135 Ratio: 0.94814813
D Npoints: 17 Uncompressed: 136 Compressed: 137 Ratio: 0.99270076
D Npoints: 18 Uncompressed: 144 Compressed: 137 Ratio: 1.0510949
D Npoints: 19 Uncompressed: 152 Compressed: 148 Ratio: 1.027027
D Npoints: 20 Uncompressed: 160 Compressed: 148 Ratio: 1.081081
D Npoints: 21 Uncompressed: 168 Compressed: 154 Ratio: 1.0909091
D Npoints: 22 Uncompressed: 176 Compressed: 160 Ratio: 1.1
    . . . 
D Npoints: 99 Uncompressed: 792 Compressed: 520 Ratio: 1.5230769

I'm not sure how you can get sizes that are 3.5 smaller but I assume that you compressed a bunch of polygons together. Compressing multiple polygons together would obviously increase the compression ratio.

Of course this is not a usable approach for a program, as it would have to keep decompressing the polygons to draw them into the screen which would require more memory, and CPU beating the whole purpose. I just wanted to get some figures...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM