一、shapefile
@Test
public void readShapefile() throws IOException {
String filePath = "F:/data/location.shp";
File file = new File(filePath);
DataStore dataStore = new ShapefileDataStore(file.toURI().toURL());//创建ShapefileDataStore实例
String[] typeNames = dataStore.getTypeNames();//获取数据源中所有可获取的图层名称
System.out.println(Arrays.toString(typeNames));
//逐个解析图层数据
for (int i = 0; i < typeNames.length; i++) {
//图层名称
String typeName = typeNames[i];
//featureSource用于读取对应图层中的Feature要素数据
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
//获取FeatureSource中的Feature集合
SimpleFeatureCollection features = featureSource.getFeatures();
//获取集合迭代器
SimpleFeatureIterator iterator = features.features();
while (iterator.hasNext()) {
SimpleFeature next = iterator.next();
//要素类
SimpleFeatureType featureType = next.getFeatureType();
//获取对应的类属性
List<AttributeDescriptor> attributeDescriptors = featureType.getAttributeDescriptors();
for (int j = 0; j < attributeDescriptors.size(); j++) {
Name name = attributeDescriptors.get(j).getName();
System.out.println(name + ":" + next.getAttribute(name));
}
}
}
}
二、geojson
GeoJSON 是一种用于编码各种地理数据结构的格式。
GeoJSON 支持以下几何类型:Point、LineString、Polygon、MultiPoint、MultiLineString 和 MultiPolygon。 具有附加属性的几何对象是特征对象。 要素集包含在FeatureCollection 对象中。
geojson有2种读取方式:
1、org.geotools.data.geojson.GeoJSONReader解析给geojson,但是它不会读取坐标系,所以不管什么坐标系的geojson文件读出来都是默认的坐标系WGS84.
2、org.geotools.geojson.feature.FeatureJSON解析geojson可以读到文件对应的坐标系。
1、GeoJSONReader读取
public static void geoJSONReader(String fileUrl) {
GeoJSONReader reader = null;
try {
reader = new GeoJSONReader(new FileInputStream(fileUrl));
SimpleFeatureCollection featureCollection = reader.getFeatures();
//创建图层数据迭代器
FeatureIterator<SimpleFeature> simpleFeatureFeatureIterator = featureCollection.features();
while (simpleFeatureFeatureIterator.hasNext()) {
AnalysisUtil.readProperty(simpleFeatureFeatureIterator.next());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、FeatureJSON读取
public static void featureJson(String fileUrl) {
// 指定GeometryJSON构造器,15位小数
FeatureJSON featureJson = new FeatureJSON(new GeometryJSON(15));
try {
FeatureCollection featureCollection = featureJson.readFeatureCollection(new FileInputStream(fileUrl));
//获取坐标系
CoordinateReferenceSystem coordinateReferenceSystem =
featureCollection.getSchema().getCoordinateReferenceSystem();
log.info("解析geojson获取坐标系:{}", coordinateReferenceSystem);
FeatureIterator featureIterator = featureCollection.features();
while (featureIterator.hasNext()) {
SimpleFeature simpleFeature = (SimpleFeature) featureIterator.next();
AnalysisUtil.readProperty(simpleFeature);
}
} catch (IOException e) {
e.printStackTrace();
}
}
三、KML
KML(Keyhole Markup Language,Keyhole 标记语言)是一种基于XML 的标记语言,利用XML 语法格式描述地理空间数据(如点、线、面、多边形和模型等)。
package org.geotools.tutorial.feature;
import com.sun.xml.internal.ws.util.UtilException;
import org.geotools.kml.KMLConfiguration;
import org.geotools.xsd.PullParser;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.xml.sax.SAXException;
import javax.xml.stream.XMLStreamException;
import java.io.FileInputStream;
import java.io.IOException;
public class AnalysisKml {
public static void main(String[] args) {
String fileUrl = "D:\\test.kml";
try (FileInputStream fileInputStream = new FileInputStream(fileUrl)) {
PullParser parser = new PullParser(new KMLConfiguration(), fileInputStream, SimpleFeature.class);
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
//kml文件声明了坐标系才能获取到,没声明获取就是null
CoordinateReferenceSystem coordinateReferenceSystem = simpleFeature.getFeatureType().getCoordinateReferenceSystem();
//log.info("解析kml获取坐标系:{}", coordinateReferenceSystem);
readKml(simpleFeature, parser);
} catch (XMLStreamException | IOException | SAXException e) {
throw new UtilException(e.getMessage());
}
}
//递归方式获取每个地块信息
public static void readKml(SimpleFeature simpleFeature, PullParser parser) throws XMLStreamException, IOException,
SAXException {
//读取属性
AnalysisUtil.readProperty(simpleFeature);
//获取下一个simpleFeature
while (simpleFeature != null && simpleFeature.getDefaultGeometry() != null) {
simpleFeature = (SimpleFeature) parser.parse();
readKml(simpleFeature, parser);
}
}
}
注:上面解析用到一个读取属性的工具类
package org.geotools.tutorial.feature;
import lombok.extern.slf4j.Slf4j;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.geometry.Geometry;
import java.util.Collection;
import java.util.Iterator;
@Slf4j
public class AnalysisUtil {
public static void readProperty(SimpleFeature simpleFeature) {
Collection<Property> properties = simpleFeature.getProperties();
Iterator<Property> iterator = properties.iterator();
int index = 0;
while (iterator.hasNext()) {
Property property = iterator.next();
//kml拿到属性值会有自带的9个属性
if (index++ > 8) {
log.info("GeoJSONReader解析geojson -->> 属性名:【{}】,属性值:【{}】,属性类型:【{}】", property.getName().toString(),
property.getValue(), property.getType().getBinding());
}
}
Object defaultGeometry = simpleFeature.getDefaultGeometry();
//wkt格式geometry
Geometry geometry = (Geometry) defaultGeometry;
log.info("wkt格式geometry:{}", geometry);
}
}