org.meteothinker
MeteoInfoLib
0.0.1-SNAPSHOT
代码
import org.meteoinfo.projection.proj4j.CRSFactory;
import org.meteoinfo.projection.proj4j.CoordinateReferenceSystem;
import org.meteoinfo.projection.proj4j.CoordinateTransform;
import org.meteoinfo.projection.proj4j.CoordinateTransformFactory;
import org.meteoinfo.projection.proj4j.ProjCoordinate;
public class Grid2WGS84Utils {
//lat_0\lon_0为中心点经纬度
private static final String LCC_PARAM = "+proj=lcc +lat_1=30.0 +lat_2=60.0 +lat_0=30.0 +lon_0=102.0 +x_0=3000 +y_0=3000 +units=m";
private static final String WGS_PARAM = "+proj=latlong +datum=WGS84";
private static final double X_MIN = -2850541D;//X轴最小值
private static final double X_DEL = 3000D;//X轴间隔3KM
private static final double Y_MIN = -1932198.7D;//Y轴最小值
private static final double Y_DEL = 3000D;//Y轴间隔3KM
public static final int xCount = 1901, yCount = 1801;//X、Y轴点个数,坐标(xIndex,yIndex)
private static final CoordinateTransform transform;
static {
CoordinateReferenceSystem src = new CRSFactory().createFromParameters("lcc", LCC_PARAM);
CoordinateReferenceSystem dst = new CRSFactory().createFromParameters("wgs", WGS_PARAM);
transform = new CoordinateTransformFactory().createTransform(src, dst);
}
private Grid2WGS84Utils() {
// empty method
}
/**
* build ProjCoordinate from grid's index
*
* @param xIndex x index, start from 0, you can use double like 180.5
* @param yIndex y index, start from 0, you can use double like 160.5
* @return the source ProjCoordinate
*/
public static ProjCoordinate buildSource(double xIndex, double yIndex) {
double x = X_MIN + X_DEL * xIndex;
double y = Y_MIN + Y_DEL * yIndex;
return new ProjCoordinate(x, y);
}
/**
*
* @param buildSource()
* @return lon、lat
*/
public static ProjCoordinate transform(ProjCoordinate src) {
ProjCoordinate tgt = new ProjCoordinate();
transform.transform(src, tgt);
return tgt;
}
public static void main(String[] args) {
// ProjCoordinate pc = buildSource(0,0);//左下角
ProjCoordinate pc = buildSource(950,900);//这不是中心点,需要自己计算
ProjCoordinate pc2 = transform(pc);
System.out.println(pc2.x+"=="+pc2.y);
}
}
可以通过验证四个角坐标确定是否正确。