1. 程式人生 > >OpenLayers系列之-Geometry格式轉換

OpenLayers系列之-Geometry格式轉換

Geometry有多種格式,例如WKT, GML, KML等格式。OpenLayers提供Geometry格式的轉換方法,可以實現不同格式的Geometry的轉換。

Geometry格式轉換類位於OpenLayers.Format名稱空間下。本文以WKT--> GML的轉換來說明OpenLayers Geometry格式轉換方法的使用。

1. 定義WKT Geometry

<Ship mmsi="209371000" position="POINT(121.7229 31.318561666666668)" shipdomain="POLYGON ((250.72289999999998 31.318561666666668, 201.0260966507277 47.09294825323749, 166.94331969198262 50.04947279928925, 150.88040212866733 50.800981724128654, 141.48677815291356 51.082439819580216, 135.01533832248563 51.21210145997425, 129.99014132220967 51.277447790156515, 125.69925693637005 51.30905792766835, 121.7229 51.31856166666667, 117.74654306362996 51.30905792766835, 113.45565867779032 51.277447790156515, 108.43046167751437 51.21210145997425, 101.95902184708645 51.082439819580216, 92.56539787133266 50.80098172412866, 76.50248030801738 50.04947279928925, 42.41970334927235 47.092948253237495, -7.277100000000004 31.318561666666653, 42.41970334927228 15.544175080095846, 76.50248030801737 12.58765053404409, 92.56539787133266 11.836141609204681, 101.95902184708643 11.554683513753115, 108.43046167751437 11.425021873359086, 113.45565867779032 11.359675543176817, 117.74654306362994 11.32806540566499, 121.7229 11.318561666666668, 125.69925693637005 11.32806540566499, 129.99014132220967 11.359675543176817, 135.0153383224856 11.425021873359086, 141.48677815291353 11.554683513753119, 150.88040212866733 11.836141609204681, 166.94331969198254 12.587650534044087, 201.0260966507276 15.544175080095835, 250.72289999999998 31.318561666666668))
"
 speed="12.3" loa="129.0" width="20.0" type="貨輪" updatetime="2015-06-15 18:38:00.0"/>
$.ajax({  
url: 'http://localhost:8080/AssistSystem/getShipsDomain.do',
type: 'GET',
dataType: 'xml',
success: function(xml) {
$(xml).find("Ship").each(function(i){

//這裡 shipdomain 為wkt 資料
var shipdomain = $(this).attr("shipdomain");
var wktFormat = new ol.format.WKT();
var feature = wktFormat.readFeature(shipdomain, {
        dataProjection: 'EPSG:4326',
        featureProjection: 'EPSG:3857'
      });
source.addFeature(feature); 
            });
}
});