【问题标题】:extract geometry in shepfile .shp在 shapefile .shp 中提取几何图形
【发布时间】:2018-05-06 14:50:45
【问题描述】:

我想从 shapefile 中提取一些数据, 我有这个可以读取 shapefile 文件的功能显示在地图上 我可以阅读一些信息,但我迷失在这个文件中提取许多信息

public class Quickstart {                 
            public static void main(String[] args) throws Exception {
                // display a data store file chooser dialog for shapefiles
                File file = JFileDataStoreChooser.showOpenFile("shp", null);
                if (file == null) {
                    return;
                }        
                FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
                //SimpleFeatureSource featureSource = dataStore.getFeatureSource();                        
                String t = dataStore.getTypeNames()[0];
                SimpleFeatureSource featureSource = dataStore.getFeatureSource(t);                
                SimpleFeatureType schema = featureSource.getSchema();
                //String geomType = schema.getGeometryDescriptor().getType().getBinding().getName();
                GeometryDescriptor geom = schema.getGeometryDescriptor();                                     
                List<AttributeDescriptor> attributes = schema.getAttributeDescriptors();
                GeometryType geomType = null;
                List<AttributeDescriptor> attribs = new ArrayList<AttributeDescriptor>();
                for (AttributeDescriptor attrib : attributes) {
                    AttributeType type = attrib.getType();
                    if (type instanceof GeometryType) {
                        geomType = (GeometryType) type;       
                    } else {
                        attribs.add(attrib);
                    }
                }
                GeometryTypeImpl gt = new GeometryTypeImpl(
                        new NameImpl("the_geom"), geomType.getBinding(),
                        geomType.getCoordinateReferenceSystem(),
                        geomType.isIdentified(), geomType.isAbstract(),
                        geomType.getRestrictions(), geomType.getSuper(),
                        geomType.getDescription());               
                GeometryDescriptor geomDesc = new GeometryDescriptorImpl(
                        gt, new NameImpl("the_geom"),
                        geom.getMinOccurs(), geom.getMaxOccurs(),
                        geom.isNillable(), geom.getDefaultValue());        
                attribs.add(0, geomDesc);           
                SimpleFeatureType shpType = new SimpleFeatureTypeImpl(
                        schema.getName(), attribs, geomDesc,
                        schema.isAbstract(), schema.getRestrictions(),
                        schema.getSuper(), schema.getDescription());                
                dataStore.createSchema(shpType);                        
                CachingFeatureSource cache = new CachingFeatureSource(featureSource);
                        // Create a map context and add our shapefile to it
                MapContext map = new DefaultMapContext();
                map.setTitle("Using cached features");
                map.addLayer(cache, null);        
                // Now display the map
                JMapFrame.showMap(map);
            }

我想提取这些信息:

 POLYGON((0.6883 49.4666,0.6836 49.4664,0.6836 49.4663,0.6841 49.466,0.6844 49.4658,0.6847 49.4653,0.685 49.465,
    0.6852 49.4646,0.6865 49.4624,0.6868 49.4621,0.6869 49.4618,0.6873 49.4617,0.6874 49.4617,0.6878 49.4616,0.6884 49.4615,
    0.6898 49.4614,0.6909 49.4613,0.6909 49.4618,0.6913 49.4618,0.6906 49.4667,0.6883 49.4666))

我不知道怎么办 请帮忙

【问题讨论】:

    标签: java geometry shapefile geotools


    【解决方案1】:

    很难看出 sn-p 是如何返回任何属性的。

    如果你想要某个功能的geometry,那么你需要使用

    Geometry geom = feature.getDefaultGeometry();
    

    要将其写为众所周知的文本 (WKT),您可以使用 .toString() 方法或使用 WKTWriter 来更好地控制格式。

    【讨论】:

    • 感谢您的回复,关于:feature.getDefault Geometry() 特征:我应该如何声明呢?谢谢
    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2022-08-17
    • 2011-01-14
    • 2014-05-29
    相关资源
    最近更新 更多