从特定单词开始从 geojson 读取
Reading from geojson starting from specific word
我知道这里已经提到了这个问题:
Start reading the file after a specific word
但是,就我而言,我想从特定单词(包括该单词)读取 .geojson 文件并将其保存在字符串中。
{"type": "FeatureCollection","features": [{"type": "Feature","properties": {},"geometry": {"type": "LineString","coordinates": [[4.354282,52.032195],[4.354087,52.032462],[4.353783,52.032962],[4.353579,52.033437],[4.353333,52.034151],[4.352991,52.03545],[4.352517,52.037002],[4.352442,52.037352],[4.352368,52.0378],[4.352336,52.038238],[4.352331,52.039962],[4.352346,52.040706]
]
}
}
]
}
我的字符串应以 {"type": "Linestring" 开头,然后是文件的其余部分。它必须适用于任何 Linestring geojson。
到目前为止我的代码:
BufferedReader br = new BufferedReader(new FileReader(
"C:\Users\****\Desktop\test2.geojson"));
String line;
while ((line = br.readLine()) != null) {
if (line.contains("LineString")) {
break; // breaks the while loop
}
}
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
任何人都可以把我推向正确的方向吗?
干杯!
下面的代码对我有用!
要到达 JSON 的所需部分,我首先必须遍历树层次结构。这意味着打开 "features"、"geometry" 最后 "coordinates" 以获取坐标。在最后添加线串和括号以再次转换为适当的 JSON 文件。
// look for features
if (jsoninput.contains("features")) {
jsonArray1 = obj.getJSONArray("features");
System.out.println(jsonArray1+ "\n");
}
// look for geometry
if (jsoninput.contains("geometry")) {
jsonArray2 = obj.getJSONObject("geometry");
System.out.println(jsonArray2+ "\n");
jsonArray3 = jsonArray2.getJSONArray("coordinates");
output = ""+jsonArray3;
System.out.println(output);
}
// no features of geometry, just check "coordinates"
if (!jsoninput.contains("features") & !jsoninput.contains("geometry")) {
jsonArray4 = obj.getJSONArray("coordinates");
output = ""+jsonArray4;
System.out.println(output);
}
// output is just a String of coordinates, add LineString prefix and
// brackets to make it a proper JSON again
String finaloutput = "{\"type\": \"LineString\",\"coordinates\":"
+ output + "}";
System.out.println(finaloutput);
我知道这里已经提到了这个问题:
Start reading the file after a specific word
但是,就我而言,我想从特定单词(包括该单词)读取 .geojson 文件并将其保存在字符串中。
{"type": "FeatureCollection","features": [{"type": "Feature","properties": {},"geometry": {"type": "LineString","coordinates": [[4.354282,52.032195],[4.354087,52.032462],[4.353783,52.032962],[4.353579,52.033437],[4.353333,52.034151],[4.352991,52.03545],[4.352517,52.037002],[4.352442,52.037352],[4.352368,52.0378],[4.352336,52.038238],[4.352331,52.039962],[4.352346,52.040706]
]
}
}
]
}
我的字符串应以 {"type": "Linestring" 开头,然后是文件的其余部分。它必须适用于任何 Linestring geojson。
到目前为止我的代码:
BufferedReader br = new BufferedReader(new FileReader(
"C:\Users\****\Desktop\test2.geojson"));
String line;
while ((line = br.readLine()) != null) {
if (line.contains("LineString")) {
break; // breaks the while loop
}
}
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
任何人都可以把我推向正确的方向吗?
干杯!
下面的代码对我有用!
要到达 JSON 的所需部分,我首先必须遍历树层次结构。这意味着打开 "features"、"geometry" 最后 "coordinates" 以获取坐标。在最后添加线串和括号以再次转换为适当的 JSON 文件。
// look for features
if (jsoninput.contains("features")) {
jsonArray1 = obj.getJSONArray("features");
System.out.println(jsonArray1+ "\n");
}
// look for geometry
if (jsoninput.contains("geometry")) {
jsonArray2 = obj.getJSONObject("geometry");
System.out.println(jsonArray2+ "\n");
jsonArray3 = jsonArray2.getJSONArray("coordinates");
output = ""+jsonArray3;
System.out.println(output);
}
// no features of geometry, just check "coordinates"
if (!jsoninput.contains("features") & !jsoninput.contains("geometry")) {
jsonArray4 = obj.getJSONArray("coordinates");
output = ""+jsonArray4;
System.out.println(output);
}
// output is just a String of coordinates, add LineString prefix and
// brackets to make it a proper JSON again
String finaloutput = "{\"type\": \"LineString\",\"coordinates\":"
+ output + "}";
System.out.println(finaloutput);