| package org.eclipse.stem.loggers.imagewriter.logger; |
| |
| import java.util.HashMap; |
| import java.util.LinkedList; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.apache.commons.collections.map.ReferenceMap; |
| import org.eclipse.stem.core.common.Identifiable; |
| import org.eclipse.stem.core.graph.NodeLabel; |
| import org.eclipse.stem.definitions.adapters.spatial.geo.LatLong; |
| import org.eclipse.stem.definitions.adapters.spatial.geo.LatLongProvider; |
| import org.eclipse.stem.definitions.adapters.spatial.geo.LatLongProviderAdapter; |
| import org.eclipse.stem.definitions.adapters.spatial.geo.LatLongProviderAdapterFactory; |
| import org.eclipse.stem.loggers.imagewriter.logger.projections.IMapProjection; |
| |
| public class PolygonHandler |
| { |
| |
| |
| private static final Map<Class<? extends IMapProjection>, Map> cachedPolygons = |
| new HashMap<Class<? extends IMapProjection>, Map>(); |
| |
| |
| |
| public static class ProjectedPolygons |
| { |
| public Identifiable identifiable; |
| public List<ProjectedPolygon> polygons = new LinkedList<ProjectedPolygon>(); |
| |
| public ProjectedPolygons() |
| { |
| |
| } |
| } |
| |
| public static class ProjectedPolygon |
| { |
| public double[] points; |
| |
| public ProjectedPolygon(int vertexCount) |
| { |
| points = new double[vertexCount*2+1]; |
| } |
| } |
| |
| |
| private static final ProjectedPolygons getProjectedPolygons(IMapProjection projection, Identifiable identifiable, LatLong points) |
| { |
| |
| ProjectedPolygons polygons = new ProjectedPolygons(); |
| polygons.identifiable = identifiable; |
| if (points != null) { |
| for (LatLong.Segment segment : points.getSegments()) { |
| if (segment.size() > 0) { |
| ProjectedPolygon poly = new ProjectedPolygon(segment.size()); |
| polygons.polygons.add(poly); |
| int vertexCount = segment.size(); |
| for (int i = 0; i < vertexCount; i++) { |
| double[] pt = projection.project(segment.latitude(i), segment.longitude(i)); |
| |
| poly.points[i*2] = pt[0]; |
| poly.points[i*2+1] = pt[1]; |
| } |
| } |
| } |
| } |
| |
| if (polygons.polygons.size() < 1) { |
| polygons = null; |
| } |
| |
| return polygons; |
| } |
| |
| |
| public static ProjectedPolygons getPolygonsForProjection(IMapProjection projection, NodeLabel label) |
| { |
| |
| Map projectionCache = cachedPolygons.get(projection.getClass()); |
| if (projectionCache == null) { |
| // create a new map entry for the projection |
| projectionCache = new ReferenceMap(ReferenceMap.SOFT, ReferenceMap.SOFT); |
| cachedPolygons.put(projection.getClass(), projectionCache); |
| } |
| |
| ProjectedPolygons retVal = null; |
| |
| Object obj = projectionCache.get(label.getIdentifiable().getURI()); |
| if (obj != null) { |
| System.out.println("LoadFromCache "+ label.getIdentifiable().getURI()); |
| |
| // We have an existing polygon set in cache. return |
| retVal = (ProjectedPolygons)obj; |
| } else { |
| |
| System.err.println("LoadFromFile "+ label.getIdentifiable().getURI()); |
| |
| // The polygon is not cached. We need to acquire the lat/lng data, project, and cache |
| LatLongProviderAdapter latLongProvider = (LatLongProviderAdapter)LatLongProviderAdapterFactory.INSTANCE.adapt(label, LatLongProvider.class); |
| |
| latLongProvider.setTarget(label.getIdentifiable()); |
| LatLong latLong = latLongProvider.getLatLong(); |
| |
| retVal = getProjectedPolygons(projection, label.getIdentifiable(), latLong); |
| if (retVal != null) { |
| projectionCache.put(label.getIdentifiable().getURI(), retVal); |
| } |
| } |
| |
| return retVal; |
| |
| |
| } |
| |
| |
| } |