Skip to content

Instantly share code, notes, and snippets.

@nutiteq
Created October 5, 2012 15:38
Show Gist options
  • Save nutiteq/3840572 to your computer and use it in GitHub Desktop.
Save nutiteq/3840572 to your computer and use it in GitHub Desktop.
Nutiteq SDK source
package com.nutiteq.projections;
import com.jhlabs.map.Point2D.Double;
import com.jhlabs.map.proj.ProjectionFactory;
import com.nutiteq.components.Bounds;
import com.nutiteq.components.ImmutableMapPos;
import com.nutiteq.components.MapPos;
import com.nutiteq.utils.Const;
public class Projection {
private final com.jhlabs.map.proj.Projection projection;
private float offsetX;
private float offsetY;
private float scaleX;
private float scaleY;
public Projection(String name, Bounds bounds) {
projection = ProjectionFactory.getNamedPROJ4CoordinateSystem(name);
calculateInternalTransform(bounds);
}
public Projection(String[] args, Bounds bounds) {
projection = ProjectionFactory.fromPROJ4Specification(args);
calculateInternalTransform(bounds);
}
private void calculateInternalTransform(Bounds bounds) {
float width = bounds.right - bounds.left;
float height = bounds.top - bounds.bottom;
offsetX = -bounds.left - width / 2;
offsetY = -bounds.bottom - height / 2;
scaleX = Const.UNIT_SIZE * 2 / width;
scaleY = Const.UNIT_SIZE * 2 / height;
}
public ImmutableMapPos fromWgs84(float lon, float lat) {
com.jhlabs.map.Point2D.Double src = new Double(lon, lat);
com.jhlabs.map.Point2D.Double dst = new Double();
projection.transform(src, dst);
return new ImmutableMapPos((float) dst.x, (float) dst.y);
}
public MapPos toWgs84(float x, float y) {
com.jhlabs.map.Point2D.Double src = new Double(x, y);
com.jhlabs.map.Point2D.Double dst = new Double();
projection.inverseTransform(src, dst);
return new MapPos((float) dst.x, (float) dst.y);
}
/**
* No part of public API
*/
public MapPos fromInternal(float x, float y) {
return new MapPos(x / scaleX - offsetX, y / scaleY - offsetY);
}
/**
* No part of public API
*/
public ImmutableMapPos toInternal(float x, float y) {
return new ImmutableMapPos((x + offsetX) * scaleX, (y + offsetY) * scaleY);
}
/**
* No part of public API
*/
public float[] internalTransformationMatrix(float x, float y, float z) {
com.jhlabs.map.Point2D.Double src = new Double(x, y);
com.jhlabs.map.Point2D.Double dst = new Double();
projection.inverseTransform(src, dst);
float scaleCoeff = (float) (1.0 / Math.cos(Math.toRadians(dst.y)));
float scaleZ = Math.min(scaleX, scaleY); // both should be the same actually
float[] transform = new float[16];
transform[0] = scaleX * scaleCoeff;
transform[5] = scaleY * scaleCoeff;
transform[10] = scaleZ * scaleCoeff;
transform[12] = (x + offsetX) * scaleX;
transform[13] = (y + offsetY) * scaleY;
transform[14] = z * scaleZ;
transform[15] = 1;
return transform;
}
/* Name of projection, usually "EPSG:xxxx"
* Should be redefined by each projection!
*/
public String name() {
return "NA";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment