gpt4 book ai didi

java - 如何使用 JAVA 代码读取 shape(.shp) 文件并显示其值

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:28 26 4
gpt4 key购买 nike

我有一个如下所示的形状文件。我使用在线转换器对其进行了转换。然后我得到这个文件的值如下

enter image description here

{"type":"Polygon","coordinates":[[[973154.7117999941,194614.3315999955],[973733.1576000005,194006.27879999578],[973685.9174000025,193958.0421999991],[973648.6635999978,193712.4756000042],[973648.052399993,193711.8941999972],[973525.3418000042,193713.4443999976],[973489.2430000007,193713.75579999387],[973434.6291999966,193713.75579999387],[973338.1589999944,193619.1521999985],[973282.610799998,193672.8328000009],[973265.806400001,193687.9265999943],[973246.2016000003,193707.22059999406],[973221.9282000065,193730.8708000034],[973190.808799997,193761.98980000615],[973150.0426000059,193801.5124000013],[973064.4640000015,193885.53419999778],[973003.7818000019,193945.2828000039],[973005.9601999968,193949.0175999999],[972911.9798000008,194040.50859999657],[972812.7090000063,194138.84540000558],[972728.6863999963,194053.8897999972],[972671.7383999974,193996.00779999793],[972693.9886000007,193973.4458000064],[972697.2559999973,193970.17900000513],[973180.8501999974,193471.33599999547],[973178.9834000021,193469.46819999814],[973156.2663999945,193449.7080000043],[973078.4679999948,193380.77760000527],[972940.6092000008,193251.3222000003],[972792.1697999984,193107.86200000346],[972726.1974000037,193043.75599999726],[972696.9449999928,193014.815200001],[972695.0771999955,193016.37060000002],[972499.9596000016,193216.46819999814],[972248.2037999928,193474.13639999926],[972136.7970000058,193588.03319999576],[972147.6886000037,193598.30220000446],[972141.7758000046,193603.90420000255],[972405.9792000055,193869.9740000069],[972333.1604000032,193941.5488000065],[972499.9596000016,194116.7502000034],[972676.0841999948,194301.77220000327],[972860.2456,194476.96639999747],[972904.5110000074,194434.79039999843],[972936.5644000024,194403.67139999568],[972963.7939999998,194430.27779999375],[973154.7117999941,194614.3315999955]]]}

这是一行数据。像这样我有46823条数据。我想将这些数据存储到我的 postgresql 表中。我的表是几何图形,它有 2 列“名称”、“几何”。我的表语法如下。

CREATE TABLE geometries (name varchar, geom geometry);

我编写了java代码来读取.shp文件。

import java.io.FileInputStream;
import java.io.IOException;
import org.nocrala.tools.gis.data.esri.shapefile.ShapeFileReader;
import org.nocrala.tools.gis.data.esri.shapefile.ValidationPreferences;
import org.nocrala.tools.gis.data.esri.shapefile.exception.InvalidShapeFileException;
import org.nocrala.tools.gis.data.esri.shapefile.header.ShapeFileHeader;
import org.nocrala.tools.gis.data.esri.shapefile.shape.AbstractShape;
public class BigFileExample {

public static void main(String[] args) throws IOException,
InvalidShapeFileException {

FileInputStream is = new FileInputStream(
"D:\\Test\\Filename.shp");
ValidationPreferences prefs = new ValidationPreferences();
prefs.setMaxNumberOfPointsPerShape(16650);
ShapeFileReader r = new ShapeFileReader(is, prefs);

ShapeFileHeader h = r.getHeader();
System.out.println("The shape type of this files is " + h.getShapeType());

int total = 0;
AbstractShape s;
while ((s = r.next()) != null) {
System.out.println(s.getShapeType());

total++;
}
System.out.println("Total shapes read: " + total);
is.close();
}}

执行此代码后,我只得到“多边形”形状。在我的形状文件中也有一些多边形。但我的代码没有给我这样的输出。另外我如何获取坐标值?我应该写什么来获取形状文件中的坐标?请帮我得到这个

最佳答案

我的问题得到了答案。我只是忽略了上面提到的代码,并为此目的使用了 geotool maven 依赖项。请引用以下链接

首先浏览此链接

http://docs.geotools.org/stable/userguide/library/data/shape.html

在上面的链接之后,请引用下面的链接

http://docs.geotools.org/stable/userguide/library/data/shape.html

你一定能找到解决办法。 :-) 如果您对此有任何疑问,我会在这里帮助您..谢谢

关于java - 如何使用 JAVA 代码读取 shape(.shp) 文件并显示其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48125530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com