用geotools在内存新建一个layer并显示

通常是通过读取某个shp文件,将shp中的图层显现出来,如下:

	File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Quickstart");
        
        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
	map.addLayer(layer);
	JMapFrame.showMap(map);

    现不想读取文件中图层,而利用geotools自己在内存中生成一个layer,并显示,方法如下:(layer中只画了一个polygon)

	MapContent map = new MapContent();
        map.setTitle("Quickstart");
        SimpleFeatureType TYPE = DataUtilities.createType("location","geom:Polygon,name:String");

        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal",TYPE);
        WKTReader2 wkt = new WKTReader2();

        featureCollection.add( SimpleFeatureBuilder.build( TYPE, new Object[]{ wkt.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))"), "name1"}, null) );
        Color color1 = Color.BLUE;
        Color color2 = Color.RED;
        Style style2 = SLD.createPolygonStyle(color1, color2.brighter(), 0.5f);
        Layer layer2 = new FeatureLayer(featureCollection,style2);
        layer2.setVisible(true);
        // Now display the map
        map.addLayer(layer2);
        //FeatureLayer layers = (FeatureLayer) map.layers().get(0);
        //System.out.println(layers);
        JMapFrame.showMap(map);

生成的图层如下:

参考资料:1.http://docs.geotools.org/latest/userguide/library/main/collection.html

   2.http://stackoverflow.com/questions/26882297/add-feature-to-existing-layer-in-geotools

   3.http://osgeo-org.1560.x6.nabble.com/Adding-geometry-to-layer-td4324729.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
很高兴回答你的问题!以下是示例代码,可以在 Android Studio 中使用 GeoTools 读取并显示 shape 文件: 首先,需要在项目的 build.gradle 文件中添加以下依赖项: ``` dependencies { implementation 'org.geotools:gt-shapefile:23.2' implementation 'org.geotools:gt-epsg-hsql:23.2' implementation 'org.geotools:gt-geojson:23.2' } ``` 然后,在你的 Activity 或 Fragment 中,可以使用以下代码读取 shape 文件并将其显示在地图上: ```java import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geometry.jts.JTS; import org.geotools.map.FeatureLayer; import org.geotools.map.MapContent; import org.geotools.styling.SLD; import org.geotools.styling.Style; import org.locationtech.jts.geom.Envelope; import org.locationtech.jts.geom.Geometry; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity { private MapContent mapContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化地图内容 mapContent = new MapContent(); // 读取 shape 文件 File shapeFile = new File("/path/to/shapefile.shp"); ShapefileDataStore dataStore = null; try { dataStore = new ShapefileDataStore(shapeFile.toURI().toURL()); } catch (IOException e) { e.printStackTrace(); } // 获取 feature source 和 feature collection SimpleFeatureSource featureSource = null; SimpleFeatureCollection featureCollection = null; try { featureSource = dataStore.getFeatureSource(); featureCollection = featureSource.getFeatures(); } catch (IOException e) { e.printStackTrace(); } // 获取 feature type 和 geometry type SimpleFeatureType featureType = featureSource.getSchema(); Class<?> geometryType = featureType.getGeometryDescriptor().getType().getBinding(); // 创建 feature layer 和 style FeatureLayer featureLayer = new FeatureLayer(featureCollection, createStyle(geometryType)); mapContent.addLayer(featureLayer); // 设置地图范围 Envelope envelope = featureCollection.getBounds(); mapContent.getViewport().setBounds(envelope); // 获取地图视图并将其添加到布局中 MapView mapView = findViewById(R.id.map_view); mapView.setMapContent(mapContent); } @Override protected void onDestroy() { super.onDestroy(); // 释放地图内容 mapContent.dispose(); } private Style createStyle(Class<?> geometryType) { // 根据几何类型创建默认样式 if (Geometry.class.isAssignableFrom(geometryType)) { return SLD.createSimpleStyle(geometryType); } else { return null; } } } ``` 请注意,上述代码中的 `/path/to/shapefile.shp` 应替换为实际的 shape 文件路径。此外,还需要在布局文件中添加一个 MapView 控件: ```xml <org.geotools.android.MapView android:id="@+id/map_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 希望这可以帮助你实现在 Android Studio 中使用 GeoTools 读取并显示 shape 文件!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值