如何使用Java正确创建一个多边形并将其写入shapefile中?

huangapple go评论85阅读模式
英文:

How to correctly create a Polygon into a shapefile with Java?

问题

我正在尝试从坐标列表创建形状文件,看起来似乎可以工作,但当我尝试在形状文件查看器中查看形状文件时,似乎我的形状中没有坐标。而且,如果我将形状文件下载为geoJSON文件,它看起来是空的,像这样:
{"type":"GeometryCollection", "geometries": []}

因此,我有一个Java类,在这个类中,我首先创建一些虚拟数据,然后创建一个Polygon SimpleFeatureType,然后将我的点列表转换为多边形,最后尝试将该多边形写入形状文件。我没有收到任何错误,并且一切看起来都在顺利运行,直到我尝试在浏览器中查看它。

这是我的类,其中包含一些调试打印:

import ...
// (代码略)

	public static void main(String[] args) throws IOException, SchemaException {
		// 初始化坐标列表
		List<PolygonPoint> locations = new ArrayList<>();
		initData(locations);

		// 创建用于坐标位置的简单特征构建器
		SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
		builder.setName("polygonFeature");
		builder.setCRS(DefaultGeographicCRS.WGS84);
		builder.add(Polygon.TYPENAME_POLYGON, Polygon.class);
		SimpleFeatureType POLYGON = builder.buildFeatureType();

		// 创建默认的特征集合
		DefaultFeatureCollection collection = new DefaultFeatureCollection();
		GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

		// 创建特征
		SimpleFeature feature = toFeature(locations, POLYGON, geometryFactory);
		collection.add(feature);
		collection.forEach(name -> System.out.println(name));
		// (代码略)

		// 创建形状文件
		File shapeFile = new File(new File("2020-").getAbsolutePath() + "shapefile.shp");
		Map<String, Serializable> params = new HashMap<>();
		params.put("url", shapeFile.toURI().toURL());
		params.put("create spatial index", Boolean.TRUE);
		ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
		ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
		dataStore.createSchema(POLYGON);
		Transaction transaction = new DefaultTransaction("create");

		// 将特征添加到形状文件
		String typeName = dataStore.getTypeNames()[0];
		SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
		if (featureSource instanceof SimpleFeatureStore) {
			SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
			featureStore.setTransaction(transaction);
			try {
				featureStore.addFeatures(collection);
				transaction.commit();
			} catch (Exception problem) {
				transaction.rollback();
			} finally {
				transaction.close();
			}
		}
	}
}

然后,将创建5个文件,但形状不正确,不显示任何内容。我在使用此工具进行测试:https://mapshaper.org/,但也可以在互联网上搜索一些形状文件在线查看器进行测试。

英文:

I am trying to create shape files from a list of coordinates and it looks like it works but when I'm trying to view the shapefile in a shapefile viewer, it looks like there's no coordinates in my shape. Also if I then download my shapefile as geoJSON it looks empty like this:
{&quot;type&quot;:&quot;GeometryCollection&quot;, &quot;geometries&quot;: []}

So I have this one Java class, where I first create some dummy data, then create a Polygon SimpleFeatureType and then convert my list of points into a polygon and eventually try to write that polygon into a shapefile. I dont get any errors and all looks like running smoothly until I try to view it in the browser.

Here's my class with some debug prints :

import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
public class Main {
private static SimpleFeature toFeature(List&lt;PolygonPoint&gt; locations, SimpleFeatureType POLYGON,
GeometryFactory geometryFactory) {
Coordinate[] coords = new Coordinate[locations.size()];
int i = 0;
for (PolygonPoint location : locations) {
Coordinate coord = new Coordinate(location.x, location.y, 0);
coords[i] = (coord);
i++;
}
Polygon polygon = geometryFactory.createPolygon(coords);
System.out.println(polygon.toString());
/**
* Degub print in this point looks like this: POLYGON ((60.15396170672204
* 24.665516804291176, 60.1548304906673 24.67437097771886, 60.156868902093464
* 24.684907435753292, 60.16053593400762 24.681227944085034, 60.16351621152651
* 24.677017850437107, 60.1641222365678 24.670587380672547, 60.163303711361145
* 24.664257803508647, 60.16193970279525 24.6590194867117, 60.158682674758644
* 24.65963863846621, 60.15396170672204 24.665516804291176)) So looks okay so
* far...
*/
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(POLYGON);
featureBuilder.add(polygon);
return featureBuilder.buildFeature(null);
}
static class PolygonPoint {
public double x;
public double y;
public PolygonPoint(double x, double y) {
this.x = x;
this.y = y;
}
}
private static void initData(List&lt;PolygonPoint&gt; locations) {
locations.add(new PolygonPoint(60.1539617067220433, 24.6655168042911761));
locations.add(new PolygonPoint(60.1548304906673010, 24.6743709777188585));
locations.add(new PolygonPoint(60.1568689020934642, 24.6849074357532920));
locations.add(new PolygonPoint(60.1605359340076191, 24.6812279440850340));
locations.add(new PolygonPoint(60.1635162115265132, 24.6770178504371067));
locations.add(new PolygonPoint(60.1641222365677990, 24.6705873806725471));
locations.add(new PolygonPoint(60.1633037113611451, 24.6642578035086473));
locations.add(new PolygonPoint(60.1619397027952516, 24.6590194867116992));
locations.add(new PolygonPoint(60.1586826747586443, 24.6596386384662090));
locations.add(new PolygonPoint(60.1539617067220433, 24.6655168042911761));
}
public static void main(String[] args) throws IOException, SchemaException {
List&lt;PolygonPoint&gt; locations = new ArrayList&lt;&gt;();
initData(locations);
// create simple feature builder for the locations
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName(&quot;polygonFeature&quot;);
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.add(Polygon.TYPENAME_POLYGON, Polygon.class);
SimpleFeatureType POLYGON = builder.buildFeatureType();
DefaultFeatureCollection collection = new DefaultFeatureCollection();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeature feature = toFeature(locations, POLYGON, geometryFactory);
collection.add(feature);
collection.forEach(name -&gt; System.out.println(name));
/**
* Degub print in this point looks like this:
* SimpleFeatureImpl:polygonFeature=[SimpleFeatureImpl.Attribute:
* Polygon&lt;Polygon id=fid-77f7c041_174f9627c85_-8000&gt;=POLYGON
* ((60.15396170672204 24.665516804291176, 60.1548304906673 24.67437097771886,
* 60.156868902093464 24.684907435753292, 60.16053593400762 24.681227944085034,
* 60.16351621152651 24.677017850437107, 60.1641222365678 24.670587380672547,
* 60.163303711361145 24.664257803508647, 60.16193970279525 24.6590194867117,
* 60.158682674758644 24.65963863846621, 60.15396170672204 24.665516804291176))]
* So looks okay still...
*/
File shapeFile = new File(new File(&quot;2020-&quot;).getAbsolutePath() + &quot;shapefile.shp&quot;);
Map&lt;String, Serializable&gt; params = new HashMap&lt;&gt;();
params.put(&quot;url&quot;, shapeFile.toURI().toURL());
params.put(&quot;create spatial index&quot;, Boolean.TRUE);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
dataStore.createSchema(POLYGON);
Transaction transaction = new DefaultTransaction(&quot;create&quot;);
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
transaction.rollback();
} finally {
transaction.close();
}
}
}
}

Then the 5 files get created as intended. However the shape is not correct and dont show anything. I was testing them with this tool: https://mapshaper.org/ but one can just google some shapefile online viewer.

答案1

得分: 2

感谢Ian Turton提供的答案链接,问题通过将以下内容替换解决:

builder.add(Polygon.TYPENAME_POLYGON, Polygon.class);

替换为:

builder.add("the_geom", Polygon.class);

英文:

Thanks to Ian Turton for linking the answer, the problem was solved by replacing this :

builder.add(Polygon.TYPENAME_POLYGON, Polygon.class);

with this:

builder.add(&quot;the_geom&quot;, Polygon.class);

答案2

得分: 1

每当我看到涉及事务但没有写入数据的情况,我会关注事务处理。你在这方面做得很好,因为在出现异常时会进行回滚。但我强烈建议你也记录一些日志消息,指示出现了什么类型的异常。特别是因为你捕获了通用的 Exception 异常。

看一下 catch 代码块:

if (featureSource instanceof SimpleFeatureStore) {
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    featureStore.setTransaction(transaction);
    try {
        featureStore.addFeatures(collection);
        transaction.commit();
    } catch (Exception problem) {
        problem.printStackTrace(System.err); // 打印出异常的堆栈跟踪信息
        transaction.rollback();
    } finally {
        transaction.close();
    }
}

这样至少可以让你有机会看到出了什么问题。

英文:

Whenever I see something with an transaction where data is not written, I look at the transaction handling. You make it actually good, as you rollback when there is an exception. But I would strongly suggest that you also make some log message indicating what kind of exception occured. Especially as you catch the generic Exception.

See the catch block:

if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace(System.err); // print out the stack trace of the exception
transaction.rollback();
} finally {
transaction.close();
}
}

This will give you at least a chance to see what went wrong.

huangapple
  • 本文由 发表于 2020年10月5日 23:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64211953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定