package org.oskari.map.userlayer.input;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.ogr.OGRDataStoreFactory;
import org.geotools.data.ogr.bridj.BridjOGRDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import fi.nls.oskari.service.ServiceException;

public class GPXParser implements FeatureCollectionParser {

    public static final String SUFFIX = "GPX";

    @Override
    public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS) throws ServiceException {
        OGRDataStoreFactory factory = new BridjOGRDataStoreFactory();
        if (!factory.isAvailable()) {
            throw new ServiceException("GDAL library is not found for GPX import");
        }

        try {
            // GPX always lon,lat 4326
            sourceCRS = CRS.decode("EPSG:4326", true);
        } catch (FactoryException e) {
            throw new ServiceException("Failed to decode EPSG:4326");
        }

        Map<String, String> connectionParams = new HashMap<>();
        connectionParams.put("DriverName", "GPX");
        connectionParams.put("DatasourceName", file.getAbsolutePath());

        DataStore store = null;
        try {
            store = factory.createDataStore(connectionParams);
            for (String typeName : store.getTypeNames()) {
                // Skip track points
                if ("track_points".equals(typeName)) {
                    continue;
                }
                SimpleFeatureSource source = store.getFeatureSource(typeName);
                return FeatureCollectionParsers.read(source, sourceCRS, targetCRS);
            }
            throw new ServiceException("Could not find any usable typeNames from GPX file");
        } catch (Exception e) {
            throw new ServiceException("GPX parsing failed", e);
        } finally {
            if (store != null) {
                store.dispose();
            }
        }
    }

}

@@ -55,16 +55,41 @@
 o.File;%0A
+import java.util.Arrays;%0A
 import j
@@ -535,16 +535,86 @@
 ystem;%0A%0A
+import fi.nls.oskari.log.LogFactory;%0Aimport fi.nls.oskari.log.Logger;%0A
 import f
@@ -723,48 +723,250 @@
    p
-ublic static final String SUFFIX = %22GPX%22
+rivate static final Logger LOG = LogFactory.getLogger(KMLParser.class);%0A%0A    public static final String SUFFIX = %22GPX%22;%0A%0A    private static final String%5B%5D TYPENAMES = %7B%0A            %22tracks%22,%0A            %22routes%22,%0A            %22waypoints%22%0A    %7D
 ;%0A%0A 
@@ -1886,30 +1886,33 @@
     
-for (
 String
- t
+%5B%5D storeT
 ypeName
- :
+s =
  sto
@@ -1928,19 +1928,17 @@
 eNames()
-) %7B
+;
 %0A       
@@ -1946,75 +1946,166 @@
     
-    // Skip track points%0A                if (%22track_points%22.equals(
+LOG.debug(%22gpx typeNames %7B%7D%22, Arrays.toString(storeTypeNames));%0A            for (String typeName : TYPENAMES) %7B%0A                if (!contains(storeTypeNames, 
 type
@@ -2260,14 +2260,44 @@
     
-return
+SimpleFeatureCollection collection =
  Fea
@@ -2354,16 +2354,226 @@
 etCRS);%0A
+                if (collection.isEmpty()) %7B%0A                    LOG.info(%22FeatureCollection was empty, typeName %7B%7D%22, typeName);%0A                %7D else %7B%0A                    return collection;%0A                %7D%0A
         
@@ -2642,22 +2642,33 @@
 ny u
-sable typeName
+n-empty FeatureCollection
 s fr
@@ -2896,10 +2896,204 @@
 %0A    %7D%0A%0A
+    private boolean contains(String%5B%5D a, String k) %7B%0A        for (String s : a) %7B%0A            if (s.equals(k)) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A
 %7D%0A

Check multiple possible typenames from GPX file