---
title: "geotools读取shapefile文件"
author: "wiselyman"
date: "2015-06-02T09:31:06Z"
tags: []
categories: ["开源软件"]
slug: "geotools-shapefile"
draft: false
lastmod: "2015-06-02T09:31:06Z"
id: 2216061
url: "https://wiselyman.iteye.com/blog/2216061"
---

依赖

```
<dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
```

 

代码

```
 File file = new File("D:\\shapefiles\\states.shp");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", file.toURI().toURL());

        DataStore dataStore = DataStoreFinder.getDataStore(map);
        String typeName = dataStore.getTypeNames()[0];

        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore
                .getFeatureSource(typeName);

        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
        FeatureIterator<SimpleFeature> features = collection.features();
            while (features.hasNext()) {
                SimpleFeature feature = features.next();
                System.out.print(feature.getID());
                System.out.print(": ");
                System.out.println(feature.getDefaultGeometryProperty().getValue());//此行输出的空间信息的wkt表达形式
            }
```