A4Lの回答に関しては、フォルダとドキュメントからデータを取得する抽出方法をいくつか追加しましたが、javaapi4kmlを使用してパッケージを使用してSpringMVCでポイントラインとポリゴンを抽出していただきありがとうございます。
<dependency>
<groupId>de.micromata.jak</groupId>
<artifactId>JavaAPIforKml</artifactId>
<version>2.2.0</version>
</dependency>
@RequestMapping(value = "/testKml", method = RequestMethod.POST)
public Map<String, Object> parseKml(@RequestBody Map<String, Object> data){
Map<String, Object> response = new HashMap<String, Object>();
List<Map<String, Object>> wktObjrow = new ArrayList<Map<String, Object>>();
String src = data.get("kmlfile").toString();
try {
URL url;
url = new URL(src);
URLConnection conn = url.openConnection();
InputStream is = url.openStream();
Assert.notNull(is);
Kml kml = Kml.unmarshal(is);
Feature feature = kml.getFeature();
Map<String, Object> geodata = new HashMap<String, Object>();
if(feature != null) {
if(feature instanceof Document) {
Document document = (Document) feature;
List<Feature> featureList = document.getFeature();
for(Feature documentFeature : featureList) {
if(documentFeature instanceof Placemark) {
geodata = new HashMap<String, Object>();
Placemark placemark = (Placemark) documentFeature;
Geometry geometry = placemark.getGeometry();
geodata = parseGeometry(geometry, documentFeature.getName().toString());
if(!geodata.isEmpty())
{
wktObjrow.add(geodata);
}
}
else if(documentFeature instanceof Folder)
{
Folder folder = (Folder) documentFeature;
List<Feature> folderfeaturList = folder.getFeature();
for(Feature folderfeature : folderfeaturList)
{
geodata = new HashMap<String, Object>();
if(folderfeature instanceof Placemark) {
Placemark placemark = (Placemark) folderfeature;
Geometry geometry = placemark.getGeometry();
//push each of return store in list
geodata = parseGeometry(geometry, placemark.getName().toString());
if(!geodata.isEmpty())
{
wktObjrow.add(geodata);
}
}
else
{
System.err.println("folderfeatures was not of type Placemark");
}
}
}
else
{
System.err.println("Was not instance of Placemark or Folder");
}
}
System.out.println("wktObjrow : "+wktObjrow);
}
else
{
System.err.println("instance of feature was Not Document");
if(feature instanceof Folder) {
Folder folder = (Folder) feature;
List<Feature> featureList = folder.getFeature();
geodata = new HashMap<String, Object>();
for(Feature documentFeature : featureList) {
if(documentFeature instanceof Placemark) {
Placemark placemark = (Placemark) documentFeature;
Geometry geometry = placemark.getGeometry();
if(documentFeature.getName().toString().length() > 0)
{
geodata = parseGeometry(geometry, documentFeature.getName().toString());
if(!geodata.isEmpty())
{
wktObjrow.add(geodata);
}
}
else
{
geodata = parseGeometry(geometry, placemark.getName().toString());
if(!geodata.isEmpty())
{
wktObjrow.add(geodata);
}
}
}else
{
System.err.println("Was not instance of Placemark");
}
}
System.out.println("wktObjrow : "+wktObjrow);
}
}
}
else
{
System.err.println("Feature was null");
response.put("Null", "Feature was null");
}
}
catch (Exception e) {
// TODO: handle exception
System.err.println("Exception @ : "+ e);
}
if(!wktObjrow.isEmpty() && wktObjrow != null)
{
response.put("data", wktObjrow);
}
return response;
}
public Map<String, Object> parseGeometry(Geometry geometry,String name) {
// <Point> <LinearRing> <Geometry> <Model> <LineString> <Polygon> <MultiGeometry>
Map<String, Object> response = new HashMap<String, Object>();
List<Map<String, Object>> wktObjrow = new ArrayList<Map<String, Object>>();
if(geometry != null) {
if(geometry instanceof Polygon) {
Polygon polygon = (Polygon) geometry;
Boundary outerBoundaryIs = polygon.getOuterBoundaryIs();
if(outerBoundaryIs != null) {
LinearRing linearRing = outerBoundaryIs.getLinearRing();
if(linearRing != null) {
List<Coordinate> coordinates = linearRing.getCoordinates();
if(coordinates != null) {
Map<String, Object> map = new HashMap<String, Object>();
ArrayList<String> wkt_lonlat = new ArrayList<String>();
for(Coordinate coordinate : coordinates) {
wkt_lonlat.add(coordinate.getLongitude()+" "+coordinate.getLatitude());
}
response.put("name",name);
response.put("category","POLYGON");
response.put("row","POLYGON(("+String.join(",", wkt_lonlat)+"))");
}
else
{
System.err.println("coordinate was null");
}
}
}
}
else if(geometry instanceof Point)
{
Point point = (Point) geometry;
List<Coordinate> coordinates = point.getCoordinates();
if(coordinates != null && !coordinates.isEmpty())
{
if(coordinates != null) {
for(Coordinate coordinate : coordinates) {
Map<String, Object> map = new HashMap<String, Object>();
response.put("lon",coordinate.getLongitude());
response.put("lat",coordinate.getLatitude());
response.put("name",name);
response.put("category","POINT");
}
}
}
}
else if(geometry instanceof LineString)
{
LineString line = (LineString) geometry;
List<Coordinate> coordinates = line.getCoordinates();
if(coordinates != null && !coordinates.isEmpty())
{
if(coordinates != null) {
Map<String, Object> map = new HashMap<String, Object>();
ArrayList<String> wkt_lonlat = new ArrayList<String>();
for(Coordinate coordinate : coordinates) {
wkt_lonlat.add(coordinate.getLongitude()+" "+coordinate.getLatitude());
}
response.put("name",name);
response.put("category","LINESTRING");
response.put("row","LINESTRING("+String.join(",", wkt_lonlat)+")");
}
}
}
else if(geometry instanceof MultiGeometry)
{
MultiGeometry multigeometry = (MultiGeometry) geometry;
for (int j = 0; j < multigeometry.getGeometry().size(); j++) {
if(multigeometry.getGeometry().get(j) instanceof LineString)
{
LineString line = (LineString) multigeometry.getGeometry().get(j);
List<Coordinate> coordinates = line.getCoordinates();
if(coordinates != null && !coordinates.isEmpty())
{
if(coordinates != null) {
Map<String, Object> map = new HashMap<String, Object>();
ArrayList<String> wkt_lonlat = new ArrayList<String>();
for(Coordinate coordinate : coordinates) {
wkt_lonlat.add(coordinate.getLongitude()+" "+coordinate.getLatitude());
}
response.put("name",name);
response.put("category","LINESTRING");
response.put("row","LINESTRING("+String.join(",", wkt_lonlat)+")");
}
}
}
else if(multigeometry.getGeometry().get(j) instanceof Point)
{
Point point = (Point) multigeometry.getGeometry().get(j);
List<Coordinate> coordinates = point.getCoordinates();
if(coordinates != null && !coordinates.isEmpty())
{
if(coordinates != null) {
for(Coordinate coordinate : coordinates) {
Map<String, Object> map = new HashMap<String, Object>();
response.put("lon",coordinate.getLongitude());
response.put("lat",coordinate.getLatitude());
response.put("name",name);
response.put("category","POINT");
}
}
}
}
else if(multigeometry.getGeometry().get(j) instanceof Polygon)
{
Polygon polygon = (Polygon) multigeometry.getGeometry().get(j);
Boundary outerBoundaryIs = polygon.getOuterBoundaryIs();
if(outerBoundaryIs != null) {
LinearRing linearRing = outerBoundaryIs.getLinearRing();
if(linearRing != null) {
List<Coordinate> coordinates = linearRing.getCoordinates();
if(coordinates != null) {
Map<String, Object> map = new HashMap<String, Object>();
ArrayList<String> wkt_lonlat = new ArrayList<String>();
for(Coordinate coordinate : coordinates) {
wkt_lonlat.add(coordinate.getLongitude()+" "+coordinate.getLatitude());
}
response.put("name",name);
response.put("category","POLYGON");
response.put("row","POLYGON(("+String.join(",", wkt_lonlat)+"))");
}
else
{
System.err.println("coordinate was null");
}
}
}
}
}
}
}
else
{
System.err.println("geometry was null");
response.put("Null", "geometry was null");
}
return response;
}