简体   繁体   English

使用java中的gson从多面geojson文件解析坐标

[英]Parsing coordinates from multipolygon geojson file using gson in java

I'm a beginner in JAVA.我是 JAVA 的初学者。 I'm trying to parse/reduce the number of coordinates in a geojson file with the help of gson library.我试图在 gson 库的帮助下解析/减少 geojson 文件中的坐标数。 I can read/get other values in the geojson file.我可以读取/获取 geojson 文件中的其他值。 But I can't parse the coordinates.但我无法解析坐标。 Please help.请帮忙。 This is the sample geojson file.这是示例 geojson 文件。

I just took only two rows of the geojson file as the file is so long and not tidy.我只取了 geojson 文件的两行,因为文件太长而且不整洁。

{ "type": "Feature", "properties": { "ID_0": 105, "ISO": "IND", "NAME_0": "India", "ID_1": 1, "NAME_1": "Andaman and Nicobar", "NL_NAME_1": "", "VARNAME_1": "Andaman & Nicobar Islands|Andaman et Nicobar|Iihas de Andama e Nicobar|Inseln Andamanen und Nikobare", 
"TYPE_1": "Union Territor", "ENGTYPE_1": "Union Territory", "filename": "", "filename_1": "", "filename_2": "", "filename_3": "", "filename_4": "" }, 
geometry": { "type": "MultiPolygon", "coordinates": 
[ [ [ [ 93.787727, 6.852640 ], [ 93.788490, 6.852571 ], [ 93.789047, 6.852571 ], [ 93.789047, 6.852291 ], 
[ 93.789673, 6.852291 ], [ 93.789879, 6.852013 ], [ 93.790154, 6.851945 ], [ 93.790642, 6.851667 ], [ 93.790779, 6.851389 ], [ 93.790916, 6.851042 ], 
[ 93.790504, 6.850625 ], [ 93.790291, 6.850347 ], [ 93.790001, 6.850279 ], [ 93.789482, 6.849903 ], [ 93.789001, 6.849485 ], [ 93.788376, 6.849137 ], 
[ 93.788017, 6.848720 ], [ 93.787781, 6.848056 ], [ 93.787506, 6.848061 ], [ 93.787216, 6.847781 ], [ 93.786392, 6.847781 ], [ 93.786110, 6.848061 ], 
[ 93.785278, 6.848061 ], [ 93.784439, 6.848900 ], [ 93.784447, 6.849166 ], [ 93.785492, 6.849930 ], [ 93.786255, 6.850625 ], [ 93.786766, 6.851172 ], 
[ 93.786942, 6.851671 ], [ 93.787376, 6.851667 ], [ 93.787376, 6.852084 ], [ 93.787308, 6.852359 ], [ 93.787376, 6.852571 ], [ 93.787727, 6.852640 ] ] ], 
[ [ [ 93.719582, 7.207501 ], [ 93.719582, 7.206870 ], [ 93.719307, 7.206460 ], [ 93.718819, 7.206391 ], [ 93.718262, 7.206530 ], [ 93.717499, 7.206950 ], 
[ 93.715836, 7.206945 ], [ 93.715553, 7.207223 ], [ 93.714996, 7.207223 ], [ 93.714722, 7.206945 ], [ 93.713608, 7.206945 ], [ 93.713333, 7.207223 ], 
[ 93.713058, 7.207221 ], [ 93.712219, 7.208055 ], [ 93.712219, 7.208889 ], [ 93.711670, 7.209441 ], [ 93.711670, 7.215278 ], [ 93.711388, 7.215556 ], 
[ 93.711388, 7.215831 ], [ 93.710983, 7.217021 ], [ 93.710213, 7.218131 ], [ 93.709717, 7.218890 ], [ 93.708611, 7.220000 ], [ 93.708611, 7.222500 ], 
[ 93.708336, 7.222778 ], [ 93.708336, 7.223610 ], [ 93.708054, 7.223888 ], [ 93.708054, 7.224166 ], [ 93.707497, 7.224722 ], [ 93.707497, 7.225000 ], 
[ 93.706665, 7.225835 ], [ 93.706665, 7.226110 ], [ 93.706390, 7.226388 ], [ 93.706390, 7.226944 ], [ 93.706673, 7.227220 ], [ 93.706673, 7.227501 ], 
[ 93.706947, 7.227779 ], [ 93.706947, 7.228054 ], [ 93.707222, 7.228332 ], [ 93.707222, 7.228611 ], [ 93.708046, 7.229440 ], [ 93.708046, 7.230001 ], 
[ 93.708527, 7.230901 ], [ 93.709091, 7.231250 ], [ 93.709717, 7.231250 ], [ 93.710136, 7.230621 ], [ 93.710625, 7.230413 ], [ 93.710831, 7.229996 ], 
[ 93.710762, 7.229233 ], [ 93.710762, 7.228401 ], [ 93.711319, 7.227706 ], [ 93.711800, 7.227291 ], [ 93.712288, 7.227011 ], [ 93.712288, 7.226596 ], 
[ 93.712219, 7.226179 ], [ 93.712639, 7.225764 ], [ 93.713120, 7.225347 ], [ 93.713539, 7.224861 ], [ 93.713676, 7.223820 ], [ 93.713608, 7.221944 ], 
[ 93.716110, 7.219444 ], [ 93.716110, 7.219166 ], [ 93.716393, 7.218888 ], [ 93.716393, 7.218329 ], [ 93.716667, 7.218056 ], [ 93.716667, 7.217778 ], 
[ 93.717216, 7.217219 ], [ 93.717224, 7.216944 ], [ 93.718330, 7.215831 ], [ 93.719093, 7.215692 ], [ 93.719376, 7.215344 ], [ 93.719864, 7.214582 ], 
[ 93.720413, 7.214095 ], [ 93.720901, 7.214026 ], [ 93.721176, 7.213609 ], [ 93.721527, 7.213122 ], [ 93.721390, 7.212707 ], [ 93.721039, 7.212221 ], 
[ 93.720558, 7.211667 ], [ 93.720276, 7.211389 ], [ 93.720276, 7.211109 ], [ 93.720001, 7.210833 ], [ 93.720001, 7.210277 ], [ 93.719719, 7.209999 ], 
[ 93.719719, 7.209167 ], [ 93.719719, 7.207984 ], [ 93.719582, 7.207501 ] ] ]]

These are the POJO classes I have created to parse.这些是我创建的用于解析的 POJO 类。

Borders.java边框.java

class Borders{
private String type;
private Properties properties;
private Geometry geometry;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public Geometry1 getGeometry() {
        return geometry;
    }

    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

}

Properties.java属性.java

class Properties{
private int ID_0;
private String ISO;
private String NAME_0;
private int ID_1;
private String NAME_1;
private String NL_NAME_1;
private String VARNAME_1;
private String TYPE_1;
private String ENGTYPE_1;
private String filename;
private String filename_1;
private String filename_2;
private String filename_3;
private String filename_4;

    public int getID_0() {
        return ID_0;
    }

    public void setID_0(int ID_0) {
        this.ID_0 = ID_0;
    }

    public String getISO() {
        return ISO;
    }

    public void setISO(String ISO) {
        this.ISO = ISO;
    }

    public String getNAME_0() {
        return NAME_0;
    }

    public void setNAME_0(String NAME_0) {
        this.NAME_0 = NAME_0;
    }

    public int getID_1() {
        return ID_1;
    }

    public void setID_1(int ID_1) {
        this.ID_1 = ID_1;
    }

    public String getNAME_1() {
        return NAME_1;
    }

    public void setNAME_1(String NAME_1) {
        this.NAME_1 = NAME_1;
    }

    public String getNL_NAME_1() {
        return NL_NAME_1;
    }

    public void setNL_NAME_1(String NL_NAME_1) {
        this.NL_NAME_1 = NL_NAME_1;
    }

    public String getVARNAME_1() {
        return VARNAME_1;
    }

    public void setVARNAME_1(String VARNAME_1) {
        this.VARNAME_1 = VARNAME_1;
    }

    public String getTYPE_1() {
        return TYPE_1;
    }

    public void setTYPE_1(String TYPE_1) {
        this.TYPE_1 = TYPE_1;
    }

    public String getENGTYPE_1() {
        return ENGTYPE_1;
    }

    public void setENGTYPE_1(String ENGTYPE_1) {
        this.ENGTYPE_1 = ENGTYPE_1;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getFilename_1() {
        return filename_1;
    }

    public void setFilename_1(String filename_1) {
        this.filename_1 = filename_1;
    }

    public String getFilename_2() {
        return filename_2;
    }

    public void setFilename_2(String filename_2) {
        this.filename_2 = filename_2;
    }

    public String getFilename_3() {
        return filename_3;
    }

    public void setFilename_3(String filename_3) {
        this.filename_3 = filename_3;
    }

    public String getFilename_4() {
        return filename_4;
    }

    public void setFilename_4(String filename_4) {
        this.filename_4 = filename_4;
    }
}

Geometry.java几何.java

class Geometry{
private String type;
private double[][][] coordinates;//I don't know how to declare as it is in multidimensional array.. Need to declare in some other format I guess..

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double[][][] getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(double[][][] coordinates) {
        this.coordinates = coordinates;
    }

mainClass.java主类.java

public class Parser {
    public void parsing() throws IOException{
        GeoJson obj = new GeoJson();   
        Gson  gson = new Gson();
        String fileData = new String(Files.readAllBytes(Paths.get("D:\\test.sample.json.txt")));
        System.out.println(fileData);
        obj = gson.fromJson(fileData, GeoJson.class);
       // double coordinates[][][] = obj.properties.geometry.getCoord();
        System.out.println("obj  "+obj.getProperties().getGeometry().getCoordinates());
    }

    public static void main(String[] a) throws IOException{
        Parser parser = new Parser();
        parser.parsing();
    }
}

这应该有效:

List<List<List<List<Double>>>>  coordinates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM