2013-11-25 841 views
2

我是新來的Java GeoTools庫,我只是試圖在地圖上繪製一個多邊形。我使用GPS座標來繪製點,但繪製得很好,但我無法弄清楚如何在它們之間繪製LineString來保存我的生活。GeoTools:在地圖上繪製多邊形

我已經檢查了geotools.org上所有的教程,也this posting但沒有用。 這應該如此複雜嗎?任何人都可以張貼繪製LineString所需的代碼片段嗎?這是我已經試過最後:

SimpleFeatureType lineType = DataUtilities.createType("LINE", "geom:LineString,name:String"); 
SimpleFeatureBuilder featureBuilderLines = new SimpleFeatureBuilder(lineType); 
SimpleFeatureCollection collectionLines = FeatureCollections.newCollection(); 

LineString line = builder.createLineString(listOfPoints); 
featureBuilderLines.add(line); 
SimpleFeature featureLine = featureBuilderLines.buildFeature(null); 
((DefaultFeatureCollection)collectionLines).add(featureLine);  
Style lineStyle = SLD.createLineStyle(Color.RED, 2.0f); 
map.addLayer(new FeatureLayer(collectionLines, lineStyle)); 

感謝,並提前和問候

+0

請添加一些代碼,以便我們可以看到您嘗試嘗試的內容。 –

+0

我添加了一些代碼。 – user3032769

+0

如果你檢查行它包含什麼? –

回答

0

你似乎是混合幾何類型,你可以試試:

import org.geotools.geometry.jts.JTSFactoryFinder; 

import com.vividsolutions.jts.geom.Coordinate; 
import com.vividsolutions.jts.geom.LineString; 

public class TestLineBuilder { 
    public static void main(String[] args) { 

    com.vividsolutions.jts.geom.GeometryFactory gFac = JTSFactoryFinder.getGeometryFactory(); 
    Coordinate[] coordinates = new Coordinate[2]; 
    coordinates[0] = new Coordinate(1,3); 
    coordinates[1] = new Coordinate(3,8); 
    LineString line =gFac.createLineString(coordinates); 
    System.out.println(line); 
    } 
} 

這給正確的排序我的答案。

+0

好吧 - 我似乎誤解了座標系的概念。我想將座標定義爲緯度/緯度。那可能嗎? – user3032769

+0

你可以把你喜歡的任何值放入座標中(我只是選擇了簡單的整數) - –

+0

這段代碼只是實現了你的LineString line = builder.createLineString(listOfPoints); –

0

一直在與此戰鬥。最後得到它的工作,將地圖保存爲一個圖像(PNG)使用來自網絡的不同片段的混合。通過JMapFrame.showMap(map);顯示地圖導致異常並崩潰。無論如何,我需要這個形象。下面顯示了具有兩點的線的示例。添加更多點的折線應該是相同的:

SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); 

b.setName("LineFeature"); 

//add a geometry property 
b.setCRS(DefaultGeographicCRS.WGS84); // set crs first 
b.add("line", LineString.class); // then add geometry 

//build the type 
final SimpleFeatureType TYPE = b.buildFeatureType(); 

SimpleFeatureBuilder featureBuilderLines = new SimpleFeatureBuilder(TYPE); 

SimpleFeatureCollection collectionLines = new DefaultFeatureCollection("internal",TYPE); 

GeometryFactory gFac = JTSFactoryFinder.getGeometryFactory(JTSFactoryFinder.EMPTY_HINTS); 
Coordinate[] coordinates = new Coordinate[2]; 

double latStart = 44.9; 
double lonStart = 14.9; 

double latEnd = 12.1; 
double lonEnd = 9.4; 

coordinates[0] = new Coordinate(lonStart, latStart); 
coordinates[1] = new Coordinate(lonEnd, latEnd); 

LineString line = gFac.createLineString(coordinates); 

featureBuilderLines.add(line); 
SimpleFeature featureLine = featureBuilderLines.buildFeature(null); 
collectionLines.add(featureLine); 

float lineWidt = 2.0f; 

Style lineStyle = SLD.createLineStyle(Color.red, lineWidth); 

SimpleFeatureSource collectionFeatureSource = new CollectionFeatureSource(collectionLines); 

map.addLayer(collectionFeatureSource, lineStyle);