下面的geojson导入postgis,shp导入postgis,postgis导出shp,postgis导出geojson已经封装好
maven里导入对应的jar(复制pom相应代码),复制PostgisUtility.java和PostgisDataStore.java文件,就可以按照app.java里的方式调用这两个类文件里封装完的四个方法。这四个方法代码如下:
validateshp,validategeojson方法为验证路径有效性,可不调用或者自己判断,否则java可能会报文件路径异常。
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>iwuang</groupId>
<artifactId>geotools4postgis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>geotools4postgis</name>
<url>https://github.com/yieryi/geotools4postgis</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<geotools.version>21-SNAPSHOT</geotools.version>
<postgresql.version>42.1.4</postgresql.version>
</properties>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
<repository>
<id>maven</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-metadata</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.postgis</groupId>
<artifactId>postgis-driver</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
PostgisDataStore类
@Data
public class PostgisDataStore {
private static DataStore postgisDataStore = null;
private static String dbtype = null;
private static String host = null;
private static String port = null;
private static String database = null;
private static String schema = null;
private static String username = null;
private static String password = null;
public PostgisDataStore() {
public static DataStore getInstance() {
if (postgisDataStore == null) {
Map<String, Object> params = new HashMap<String, Object>();
params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbtype);
params.put(PostgisNGDataStoreFactory.HOST.key, host);
params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));
params.put(PostgisNGDataStoreFactory.DATABASE.key, database);
params.put(PostgisNGDataStoreFactory.SCHEMA.key, schema);
params.put(PostgisNGDataStoreFactory.USER.key, username);
params.put(PostgisNGDataStoreFactory.PASSWD.key, password);
try {
postgisDataStore = DataStoreFinder.getDataStore(params);
logger.info("\nPostgisDataStore 初始化geotools中的 Datastore成功\n");
} catch (IOException e) {
logger.error("\nPostgisDataStore 初始化geotools中的 Datastore失败\n");
logger.error(e.getMessage());
return postgisDataStore;
shp文件导入到postgis数据库中
public static boolean importShp(String shppath, String tablename) throws IOException {
if (!validateShp(shppath, true)) return false;
DataStore pgDatastore = PostgisDataStore.getInstance();
ShapefileDataStore shapefileDataStore = null;
shapefileDataStore = new ShapefileDataStore(new File(shppath).toURI().toURL());
shapefileDataStore.setCharset(Charset.forName("utf-8"));
FeatureSource featureSource = shapefileDataStore.getFeatureSource();
FeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureType shpfeaturetype = shapefileDataStore.getSchema();
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.init(shpfeaturetype);
typeBuilder.setName(tablename);
SimpleFeatureType newtype = typeBuilder.buildFeatureType();
pgDatastore.createSchema(newtype);
logger.info("\npostgis创建数据表成功");
FeatureIterator iterator = featureCollection.features();
FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = pgDatastore.getFeatureWriterAppend(tablename, Transaction.AUTO_COMMIT); while (iterator.hasNext()) {
Feature feature = iterator.next();
SimpleFeature simpleFeature = featureWriter.next();
Collection<Property> properties = feature.getProperties();
Iterator<Property> propertyIterator = properties.iterator();
while (propertyIterator.hasNext()) {
Property property = propertyIterator.next();
simpleFeature.setAttribute(property.getName().toString(), property.getValue());
featureWriter.write();
iterator.close();
featureWriter.close();
shapefileDataStore.dispose();
pgDatastore.dispose();
logger.info("\nshp导入postgis成功");
return true;
把geojson格式数据导入到postgsi,存储坐标和所有属性
public static boolean importGeojson(String geojsonpath, String tablename) throws IOException {
if (!validateGeojson(geojsonpath, true)) return false;
DataStore pgDatastore = PostgisDataStore.getInstance();
FeatureJSON featureJSON = new FeatureJSON();
FeatureCollection featureCollection = featureJSON.readFeatureCollection(new FileInputStream(geojsonpath));
SimpleFeatureType geojsontype = (SimpleFeatureType) featureCollection.getSchema();
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.init(geojsontype);
typeBuilder.setName(tablename);
SimpleFeatureType newtype = typeBuilder.buildFeatureType();
pgDatastore.createSchema(newtype);
FeatureIterator iterator = featureCollection.features();
FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = pgDatastore.getFeatureWriterAppend(tablename, Transaction.AUTO_COMMIT); while (iterator.hasNext()) {
Feature feature = iterator.next();
SimpleFeature simpleFeature = featureWriter.next();
Collection<Property> properties = feature.getProperties();
Iterator<Property> propertyIterator = properties.iterator();
while (propertyIterator.hasNext()) {
Property property = propertyIterator.next();
simpleFeature.setAttribute(property.getName().toString(), property.getValue());
featureWriter.write();
iterator.close();
featureWriter.close();
pgDatastore.dispose();
return true;
导出postgis数据库中的指定矢量表为shp文件
public static boolean exportShp(String tablename, String shpPath) throws IOException, FactoryException {
DataStore pgDatastore = PostgisDataStore.getInstance();
FeatureSource featureSource = pgDatastore.getFeatureSource(tablename);
FeatureCollection featureCollection = featureSource.getFeatures();
FeatureIterator<SimpleFeature> iterator = featureCollection.features();
SimpleFeatureType pgfeaturetype = pgDatastore.getSchema(tablename);
File file = new File(shpPath);
if (!validateShp(shpPath, false)) return false;
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
ShapefileDataStore shpDataStore = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
String srid = pgfeaturetype.getGeometryDescriptor().getUserData().get("nativeSRID").toString();
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.init(pgfeaturetype);
if (!srid.equals("0")) {
CoordinateReferenceSystem crs = CRS.decode("EPSG:" + srid, true);
typeBuilder.setCRS(crs);
pgfeaturetype = typeBuilder.buildFeatureType();
shpDataStore.setCharset(Charset.forName("utf-8"));
shpDataStore.createSchema(pgfeaturetype);
FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = shpDataStore.getFeatureWriter(shpDataStore.getTypeNames()[0], AUTO_COMMIT);
while (iterator.hasNext()) {
Feature feature = iterator.next();
SimpleFeature simpleFeature = featureWriter.next();
Collection<Property> properties = feature.getProperties();
Iterator<Property> propertyIterator = properties.iterator();
while (propertyIterator.hasNext()) {
Property property = propertyIterator.next();
if (geomfield(property.getName().toString())) {
simpleFeature.setAttribute("the_geom", property.getValue());
continue;
simpleFeature.setAttribute(property.getName().toString(), property.getValue());
featureWriter.write();
iterator.close();
featureWriter.close();
pgDatastore.dispose();
return true;
导出postgis数据库中的指定矢量表为geojson文件
public static boolean exportGeojson(String tablename, String geojsonPath) throws IOException, FactoryException {
DataStore pgDatastore = PostgisDataStore.getInstance();
FeatureSource featureSource = pgDatastore.getFeatureSource(tablename);
FeatureCollection featureCollection = featureSource.getFeatures();
FeatureJSON featureJSON = new FeatureJSON();
File file = new File(geojsonPath);
if (!file.exists()) {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file, false);
featureJSON.writeFeatureCollection(featureCollection, outputStream);
pgDatastore.dispose();
return true;
下面的geojson导入postgis,shp导入postgis,postgis导出shp,postgis导出geojson已经封装好maven里导入对应的jar(复制pom相应代码),复制PostgisUtility.java和PostgisDataStore.java文件,就可以按照app.java里的方式调用这两个类文件里封装完的四个方法。这四个方法代码如下:validateshp,validategeojson方法为验证路径有效性,可不调用或者自己判断,否则java可能会报文件路径异常。pom
上期我介绍了geotools读取shp文件的事例 (geotools系列1-读取shp文件),本次说明读取读取postgis里表数据的代码,依旧是读取数据打印出来展示。
1、环境,maven依赖等,参见上次 geotools系列1-读取shp文件 。
2、直接上java代码
package com.jjxliu.geotools.geotools_t1;
import ...
1. geotools简介
geotools是java语言封装的空间数据框架。类似于spring之与java web项目,geotools提供丰富的GIS组件,可用于快速二次搭建GIS平台。详细可登录geotools官网了解其功能信息(https://geotools.org/)。
2. geotools postgis源码解析
postgis是postgresql数据库的扩展,可以理解为postgresql的GIS化功能封装。
本博文用postgresql 11和postgis 3.1.1版本给大
GeoJson是一种对各种地理数据结构进行编码的格式,基于json的地理空间信息数据交换格式。GeoJson对象可以用来表示几何,特征或者特征集合。支持地理点、线、面、多点、多线、多面及几何集合。GeoJson不是本文的重点,因此不再赘述。
PostGIS是在对象关系型数据库PostgreSQL上增加了存储管理空间数据的能力的开源GIS数据库。本文的例子不能直接运行在PostgreSql的数据库上,需要装扩展依赖才行(具体可参考某度的详细教程)。
本......
本文主要介绍了geojsonio包导入、使用rgdal包、使用sf包导入、geojson、topojson等相关内容。本文来自微信公众号-数据小魔方,由火龙果软件Anna编辑、推荐。以前在处理gis数据的时候,都是直接导入本地shp素材、本地geojson素材,本地topojson素材,自从接触postgis数据之后,深感使用规范的存储系统来统一管理gis数据的好处,特别是数据量大了之后,优势便更加明显,你可以选择将很多需要做空间计算的步骤转移到Postgis数据库内进行计算,要知道Postgis提供的空间计算能力与R和Python这种应用导向的工具相比,优势要大得多。在批量导入素材之前,我们
Geotools操作GeoJSON:解析FeatureCollection对象文件一、解析FeatureCollection对象文件1.1 geotools操作GeoJSON过程中的问题及相关源码(转载自:Shanks7529)1.2 方法二:读取本地txt文件进行解析
GeoJSON是基于JavaScript的对象的地理信息数据格式。
GeoJSON格式示例:
"type":"Fea...
GeoTools是一个开源的Java工具包,可用于处理地理空间数据。GeoTools提供了对GeoJSON格式的支持,可以使用GeoTools解析GeoJSON数据。
以下是一个简单的示例代码,展示了如何使用GeoTools解析GeoJSON数据:
```java
File geojsonFile = new File("path/to/geojson/file"); // GeoJSON文件路径
// 创建GeoJSON文件读取器
GeoJSONDataStoreFactory dataStoreFactory = new GeoJSONDataStoreFactory();
Map<String, Object> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URLP.key, geojsonFile.toURI().toURL());
GeoJSONDataStore dataStore = (GeoJSONDataStore) dataStoreFactory.createDataStore(params);
// 获取GeoJSON数据源中的FeatureCollection
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
// 遍历FeatureCollection
try (SimpleFeatureIterator featureIterator = featureCollection.features()) {
while (featureIterator.hasNext()) {
SimpleFeature feature = featureIterator.next();
Geometry geometry = (Geometry) feature.getDefaultGeometry(); // 获取几何对象
// 对几何对象进行操作
// ...
上述代码首先创建了一个GeoJSON文件读取器,然后使用该读取器获取GeoJSON数据源中的FeatureCollection。接着遍历FeatureCollection,对其中的每个Feature进行操作。
需要注意的是,使用GeoTools解析GeoJSON数据时,需要先将GeoJSON文件转换为GeoJSON数据源,然后再从数据源中获取FeatureCollection。GeoTools支持的数据源类型包括文件、URL、InputStream等。