| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.util; |
| 14 |
|
| 15 |
import java.awt.Polygon; |
| 16 |
import java.awt.Rectangle; |
| 17 |
import java.awt.Shape; |
| 18 |
import java.awt.geom.Area; |
| 19 |
import java.awt.geom.Path2D; |
| 20 |
import java.awt.geom.PathIterator; |
| 21 |
import java.io.File; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Collections; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
import org.openstreetmap.osmosis.core.filter.common.PolygonFileReader; |
| 27 |
|
| 28 |
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; |
| 29 |
import uk.me.parabola.imgfmt.Utils; |
| 30 |
import uk.me.parabola.imgfmt.app.Coord; |
| 31 |
import uk.me.parabola.log.Logger; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class Java2DConverter { |
| 42 |
private static final Logger log = Logger.getLogger(Java2DConverter.class); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static Area createBoundsArea(uk.me.parabola.imgfmt.app.Area bbox) { |
| 52 |
return createArea(bbox.toCoords()); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static uk.me.parabola.imgfmt.app.Area createBbox(Shape shape) { |
| 63 |
Rectangle areaBounds = shape.getBounds(); |
| 64 |
return new uk.me.parabola.imgfmt.app.Area(areaBounds.y,areaBounds.x,(int) areaBounds.getMaxY(),(int) areaBounds.getMaxX()); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static Area createArea(List<Coord> polygonPoints) { |
| 76 |
return new Area(createPath2D(polygonPoints)); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static Path2D createPath2D (List<Coord> polygonPoints) { |
| 87 |
int n = polygonPoints.size(); |
| 88 |
if (n < 3) |
| 89 |
return new Path2D.Double(); |
| 90 |
|
| 91 |
Path2D path = new Path2D.Double(PathIterator.WIND_NON_ZERO, n); |
| 92 |
if (polygonPoints.get(0).highPrecEquals(polygonPoints.get(n-1))){ |
| 93 |
|
| 94 |
|
| 95 |
--n; |
| 96 |
} |
| 97 |
int lastLat = Integer.MAX_VALUE,lastLon = Integer.MAX_VALUE; |
| 98 |
for (int i = 0; i < n; i++){ |
| 99 |
Coord co = polygonPoints.get(i); |
| 100 |
int latHp = co.getHighPrecLat(); |
| 101 |
int lonHp = co.getHighPrecLon(); |
| 102 |
double x = (double)lonHp / (1<<Coord.DELTA_SHIFT); |
| 103 |
double y = (double)latHp / (1<<Coord.DELTA_SHIFT); |
| 104 |
if (i == 0) |
| 105 |
path.moveTo(x, y); |
| 106 |
else { |
| 107 |
if (lastLon != lonHp || lastLat != latHp) |
| 108 |
path.lineTo(x, y); |
| 109 |
} |
| 110 |
lastLon = lonHp; |
| 111 |
lastLat = latHp; |
| 112 |
} |
| 113 |
path.closePath(); |
| 114 |
return path; |
| 115 |
} |
| 116 |
|
| 117 |
public static Polygon createHighPrecPolygon(List<Coord> points) { |
| 118 |
Polygon polygon = new Polygon(); |
| 119 |
for (Coord co : points) { |
| 120 |
polygon.addPoint(co.getHighPrecLon(), co.getHighPrecLat()); |
| 121 |
} |
| 122 |
return polygon; |
| 123 |
} |
| 124 |
|
| 125 |
public static List<Area> areaToSingularAreas(Area area) { |
| 126 |
return areaToSingularAreas(0, area); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private static List<Area> areaToSingularAreas(int depth, Area area) { |
| 136 |
if (area.isEmpty()) { |
| 137 |
return Collections.emptyList(); |
| 138 |
} else if (area.isSingular()) { |
| 139 |
return Collections.singletonList(area); |
| 140 |
} else { |
| 141 |
List<Area> singularAreas = new ArrayList<>(); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
double[] res = new double[6]; |
| 147 |
PathIterator pit = area.getPathIterator(null); |
| 148 |
Path2D path = null; |
| 149 |
while (!pit.isDone()) { |
| 150 |
int type = pit.currentSegment(res); |
| 151 |
double lat = res[1]; |
| 152 |
double lon = res[0]; |
| 153 |
|
| 154 |
switch (type) { |
| 155 |
case PathIterator.SEG_LINETO: |
| 156 |
path.lineTo(lon, lat); |
| 157 |
break; |
| 158 |
case PathIterator.SEG_CLOSE: |
| 159 |
path.closePath(); |
| 160 |
Area a = new Area(path); |
| 161 |
if (!a.isEmpty()) { |
| 162 |
if (depth < 10 && !a.isSingular()){ |
| 163 |
|
| 164 |
singularAreas.addAll(areaToSingularAreas(depth+1,a)); |
| 165 |
} |
| 166 |
else |
| 167 |
singularAreas.add(a); |
| 168 |
} |
| 169 |
path = null; |
| 170 |
break; |
| 171 |
case PathIterator.SEG_MOVETO: |
| 172 |
path = new Path2D.Double(); |
| 173 |
path.moveTo(lon, lat); |
| 174 |
break; |
| 175 |
default: |
| 176 |
log.error("Unsupported path iterator type " + type |
| 177 |
+ ". This is an mkgmap error."); |
| 178 |
} |
| 179 |
pit.next(); |
| 180 |
} |
| 181 |
return singularAreas; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public static List<Coord> singularAreaToPoints(Area area) { |
| 193 |
return singularAreaToPoints(area, null); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static List<Coord> singularAreaToPoints(Area area, Long2ObjectOpenHashMap<Coord> coordPool) { |
| 205 |
if (area.isEmpty()) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
List<Coord> points = null; |
| 210 |
|
| 211 |
double[] res = new double[6]; |
| 212 |
PathIterator pit = area.getPathIterator(null); |
| 213 |
int prevLatHp = Integer.MIN_VALUE; |
| 214 |
int prevLongHp = Integer.MIN_VALUE; |
| 215 |
|
| 216 |
while (!pit.isDone()) { |
| 217 |
int type = pit.currentSegment(res); |
| 218 |
|
| 219 |
int latHp = (int)Math.round(res[1] * (1<<Coord.DELTA_SHIFT)); |
| 220 |
int lonHp = (int)Math.round(res[0] * (1<<Coord.DELTA_SHIFT)); |
| 221 |
|
| 222 |
switch (type) { |
| 223 |
case PathIterator.SEG_MOVETO: |
| 224 |
if (points != null) |
| 225 |
log.error("area not singular"); |
| 226 |
points = new ArrayList<>(); |
| 227 |
points.add(Coord.makeHighPrecCoord(latHp, lonHp, coordPool)); |
| 228 |
break; |
| 229 |
case PathIterator.SEG_LINETO: |
| 230 |
assert points != null; |
| 231 |
if (prevLatHp != latHp || prevLongHp != lonHp) |
| 232 |
points.add(Coord.makeHighPrecCoord(latHp, lonHp, coordPool)); |
| 233 |
break; |
| 234 |
case PathIterator.SEG_CLOSE: |
| 235 |
assert points != null; |
| 236 |
if (points.size() < 3) |
| 237 |
points = null; |
| 238 |
else { |
| 239 |
if (points.get(0).highPrecEquals(points.get(points.size() - 1))) { |
| 240 |
|
| 241 |
points.set(points.size() - 1, points.get(0)); |
| 242 |
} |
| 243 |
else |
| 244 |
points.add(points.get(0)); |
| 245 |
if (points.size() < 4) |
| 246 |
points = null; |
| 247 |
else |
| 248 |
return points; |
| 249 |
} |
| 250 |
break; |
| 251 |
default: |
| 252 |
log.error("Unsupported path iterator type " + type |
| 253 |
+ ". This is an mkgmap error."); |
| 254 |
} |
| 255 |
|
| 256 |
prevLatHp = latHp; |
| 257 |
prevLongHp = lonHp; |
| 258 |
|
| 259 |
pit.next(); |
| 260 |
} |
| 261 |
return points; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static List<List<Coord>> areaToShapes(java.awt.geom.Area area) { |
| 274 |
return areaToShapes(area, null); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public static List<List<Coord>> areaToShapes(java.awt.geom.Area area, Long2ObjectOpenHashMap<Coord> coordPool) { |
| 289 |
List<List<Coord>> outputs = new ArrayList<>(4); |
| 290 |
|
| 291 |
double[] res = new double[6]; |
| 292 |
PathIterator pit = area.getPathIterator(null); |
| 293 |
|
| 294 |
List<Coord> coords = null; |
| 295 |
|
| 296 |
int prevLatHp = Integer.MIN_VALUE; |
| 297 |
int prevLongHp = Integer.MIN_VALUE; |
| 298 |
|
| 299 |
while (!pit.isDone()) { |
| 300 |
int type = pit.currentSegment(res); |
| 301 |
|
| 302 |
int latHp = (int) Math.round(res[1] * (1<<Coord.DELTA_SHIFT)); |
| 303 |
int lonHp = (int) Math.round(res[0] * (1<<Coord.DELTA_SHIFT)); |
| 304 |
|
| 305 |
switch (type) { |
| 306 |
case PathIterator.SEG_LINETO: |
| 307 |
if (prevLatHp != latHp || prevLongHp != lonHp) |
| 308 |
coords.add(Coord.makeHighPrecCoord(latHp, lonHp, coordPool)); |
| 309 |
|
| 310 |
prevLatHp = latHp; |
| 311 |
prevLongHp = lonHp; |
| 312 |
break; |
| 313 |
case PathIterator.SEG_MOVETO: |
| 314 |
case PathIterator.SEG_CLOSE: |
| 315 |
if ((type == PathIterator.SEG_MOVETO && coords != null) || type == PathIterator.SEG_CLOSE) { |
| 316 |
if (coords.size() > 2){ |
| 317 |
if (coords.get(0).highPrecEquals(coords.get(coords.size() - 1))){ |
| 318 |
|
| 319 |
coords.set(coords.size() - 1, coords.get(0)); |
| 320 |
} |
| 321 |
else |
| 322 |
coords.add(coords.get(0)); |
| 323 |
} |
| 324 |
if (coords.size() > 3){ |
| 325 |
outputs.add(coords); |
| 326 |
} |
| 327 |
} |
| 328 |
if (type == PathIterator.SEG_MOVETO){ |
| 329 |
coords = new ArrayList<>(); |
| 330 |
coords.add(Coord.makeHighPrecCoord(latHp, lonHp, coordPool)); |
| 331 |
prevLatHp = latHp; |
| 332 |
prevLongHp = lonHp; |
| 333 |
} else { |
| 334 |
coords = null; |
| 335 |
prevLatHp = Integer.MIN_VALUE; |
| 336 |
prevLongHp = Integer.MIN_VALUE; |
| 337 |
} |
| 338 |
break; |
| 339 |
default: |
| 340 |
log.error("Unsupported path iterator type " + type |
| 341 |
+ ". This is an mkgmap error."); |
| 342 |
} |
| 343 |
|
| 344 |
pit.next(); |
| 345 |
} |
| 346 |
|
| 347 |
return outputs; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static java.awt.geom.Area AreaDegreesToMapUnit(java.awt.geom.Area area){ |
| 356 |
if (area == null) |
| 357 |
return null; |
| 358 |
double[] res = new double[6]; |
| 359 |
Path2D path = new Path2D.Double(); |
| 360 |
PathIterator pit = area.getPathIterator(null); |
| 361 |
while (!pit.isDone()) { |
| 362 |
int type = pit.currentSegment(res); |
| 363 |
|
| 364 |
double fLat = res[1]; |
| 365 |
double fLon = res[0]; |
| 366 |
int lat = Utils.toMapUnit(fLat); |
| 367 |
int lon = Utils.toMapUnit(fLon); |
| 368 |
|
| 369 |
switch (type) { |
| 370 |
case PathIterator.SEG_LINETO: |
| 371 |
path.lineTo(lon, lat); |
| 372 |
break; |
| 373 |
case PathIterator.SEG_MOVETO: |
| 374 |
path.moveTo(lon, lat); |
| 375 |
break; |
| 376 |
case PathIterator.SEG_CLOSE: |
| 377 |
path.closePath(); |
| 378 |
break; |
| 379 |
default: |
| 380 |
System.out.println("Unsupported path iterator type " + type |
| 381 |
+ ". This is an internal splitter error."); |
| 382 |
} |
| 383 |
|
| 384 |
pit.next(); |
| 385 |
} |
| 386 |
return new java.awt.geom.Area(path); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public static java.awt.geom.Area readPolyFile(String polygonFile) { |
| 395 |
File f = new File(polygonFile); |
| 396 |
if (!f.exists()) { |
| 397 |
throw new IllegalArgumentException("polygon file doesn't exist: " + polygonFile); |
| 398 |
} |
| 399 |
try { |
| 400 |
PolygonFileReader pfr = new PolygonFileReader(f); |
| 401 |
java.awt.geom.Area polygonInDegrees = pfr.loadPolygon(); |
| 402 |
return Java2DConverter.AreaDegreesToMapUnit(polygonInDegrees); |
| 403 |
} catch (Exception e) { |
| 404 |
log.error("cannot read polygon file", polygonFile); |
| 405 |
log.error(e); |
| 406 |
} |
| 407 |
return null; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
} |