java利用arcgis service rest服務進行座標投影轉換

轉換參數說明

arcgis service rest的project服務參數java

    Input Spatial Reference(inSR):輸入座標系json

    Output Spatial Reference(outSR):輸出座標系工具

    Geometries(geometries):輸入轉換的座標JSON,能夠是點、線、面測試

    Datum Transformation(transformation):座標轉換參數this

    Transform Forward(transformForward):是否參數轉換?spa

    Format(f):輸出類型JSON或者HTMLrest

定義Geometries類

首先定義一個RestGeometries類,方便生成Geometries的json字符串code

import java.util.ArrayList;
import java.util.List;

public class RestGeometries {

	private String geometryType;

	private List<Rings> geometries = new ArrayList<Rings>();

	public String getGeometryType() {
		return geometryType;
	}

	public void setGeometryType(String geometryType) {
		this.geometryType = geometryType;
	}

	public List<Rings> getGeometries() {
		return geometries;
	}

	public void setGeometries(List<Rings> geometries) {
		this.geometries = geometries;
	}
}

其中geometryType 能夠是orm

<esriGeometryPoint | esriGeometryMultipoint | esriGeometryPolyline | esriGeometryPolygon>

另外上面涉及一個類Rings用於存儲座標點server

import java.util.ArrayList;
import java.util.List;

public class Rings {
	
	private List<List<double[]>> rings = new ArrayList<List<double[]>>();
	
	public List<List<double[]>> getRings() {
		return rings;
	}

	public void setRings(List<List<double[]>> rings) {
		this.rings = rings;
	}
}

測試一下,生成一個Geometries

public String makeGeometries() {
		RestGeometries g = new RestGeometries();
		g.setGeometryType("esriGeometryPolygon");
		double[] point11 = new double[] { -117,34 };
		double[] point12 = new double[] { -116,34 };
		double[] point13 = new double[] { -117,33 };
		double[] point14 = new double[] { -117,34 };
		double[] point15 = new double[] { -115,44 };

		List<double[]> ring1 = new ArrayList<double[]>();
		ring1.add(point11);
		ring1.add(point12);
		ring1.add(point13);
		ring1.add(point14);
		ring1.add(point15);
		
		double[] point21 = new double[] { 32,17 };
		double[] point22 = new double[] { 31,17 };
		double[] point23 = new double[] { 30,17 };
		double[] point24 = new double[] { 30,16 };
		double[] point25 = new double[] { 32,17 };
		
		List<double[]> ring2 = new ArrayList<double[]>();
		ring2.add(point21);
		ring2.add(point22);
		ring2.add(point23);
		ring2.add(point24);
		ring2.add(point25);

		Rings rings1 = new Rings();
		rings1.getRings().add(ring1);
		Rings rings2 = new Rings();
		rings2.getRings().add(ring2);
		g.getGeometries().add(rings1);
		g.getGeometries().add(rings2);

		return JSON.toJSONString(g);
}

輸出:

{"geometries": 
    [
        {"rings": [[[-117,34],[-116,34],[-117,33],[-117,34],[-115,44]]]},
        {"rings": [[[32,17],[31,17],[30,17],[30,16],[32,17]]]}
    ],
    "geometryType": "esriGeometryPolygon"
}

利用httpclient訪問Rest進行座標轉換

public String project() {
		//StringBuffer sb = new StringBuffer();
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer/project");
		// 設置HTTP POST請求參數必須用NameValuePair
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("f", "json"));// format設置成json
		params.add(new BasicNameValuePair("inSR", "4326"));
		params.add(new BasicNameValuePair("outSR", "3857"));
		params.add(new BasicNameValuePair("geometries", makeGeometries()));
        //params.add(new BasicNameValuePair("transformation", sb.toString()));
		//params.add(new BasicNameValuePair("transformForward", "true"));
		try {
			HttpEntity entity = new UrlEncodedFormEntity(params);
			request.setEntity(entity);
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {// 若是狀態碼爲200,就是正常返回
				return EntityUtils.toString(response.getEntity());
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
		}
		return null;
	}

輸出

{"geometries":[
   {"rings":[[[-12913060.932019727,4028802.0261344141],
              [-13024380.422813002,3895303.9633939015],
              [-13024380.422813002,4028802.0261344141],
              [-12913060.932019727,4028802.0261344141]]]},
   {"rings":[[[3562223.7053847606,1920825.040377473],
              [3339584.7237982131,1804722.7662572993],
              [3339584.7237982131,1920825.040377473],
              [3450904.2145914868,1920825.040377473],
              [3562223.7053847606,1920825.040377473]]]}
     ]
}

關於Datum Transformation(transformation) 參數

一般咱們說的七參數轉換,能夠經過給這個參數賦值來進行座標轉換,這個參數是個JSON字符串,能夠經過如下方法賦值進行座標轉換:

String dataNum = "{'wkt':GEOGTRAN[\"MGI_To_ETRS_1989_4\",GEOGCS[\"GCS_MGI\",DATUM[\"D_MGI\",SPHEROID[\"Bessel_1841\",6377397.155,299.1528128]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],GEOGCS[\"GCS_ETRS_1989\",DATUM[\"D_ETRS_1989\",SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],METHOD[\"Coordinate_Frame\"],PARAMETER[\"X_Axis_Translation\",601.705],PARAMETER[\"Y_Axis_Translation\",84.263],PARAMETER[\"Z_Axis_Translation\",485.227],PARAMETER[\"X_Axis_Rotation\",-4.7354],PARAMETER[\"Y_Axis_Rotation\",-1.3145],PARAMETER[\"Z_Axis_Rotation\",-5.393],PARAMETER[\"Scale_Difference\",-2.3887]]}"
params.add(new BasicNameValuePair("transformation", dataNum));
params.add(new BasicNameValuePair("transformForward", "true"));

arcgis中能夠經過toolbox提供的工具建立一個自定義的transformation

相關文章
相關標籤/搜索