在shepfile .shp中提取几何体

时间:2018-05-06 14:50:45

标签: java geometry shapefile geotools

我想从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))

我不知道该怎么做 请帮忙

1 个答案:

答案 0 :(得分:1)

很难看出该片段是如何为您提供任何属性的。

如果您需要某个功能的geometry,则需要使用

Geometry geom = feature.getDefaultGeometry();

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