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

How to read a shape(.shp) file and display its values USING JAVA CODE

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

{"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 table。我的 table 是几何图形,它有 2 列 'name'、'geom'。我的 table 语法如下。

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();
  }}

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

我得到了问题的答案。我只是忽略了上面提到的代码,为此我使用了 geotool maven 依赖项。请参考下面link

先过一遍这个link

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

上面link之后请参考下面link

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

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