package nl.ovapi.rid.gtfsrt.services;

import javax.inject.Singleton;

import org.osgeo.proj4j.CRSFactory;
import org.osgeo.proj4j.CoordinateReferenceSystem;
import org.osgeo.proj4j.CoordinateTransform;
import org.osgeo.proj4j.CoordinateTransformFactory;
import org.osgeo.proj4j.ProjCoordinate;

import com.google.transit.realtime.GtfsRealtime.Position;

@Singleton
public class GeometryService {
	private CoordinateReferenceSystem wgs84;
	private CoordinateReferenceSystem rijksdriehoek;
	private CoordinateTransform factory;

	public GeometryService(){
		wgs84 = new CRSFactory().createFromName("EPSG:4326");
		rijksdriehoek = new CRSFactory().createFromParameters("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +towgs84=565.2369,50.0087,465.658,-0.406857330322398,0.350732676542563,-1.8703473836068,4.0812 +no_defs no_defs");
		factory = new CoordinateTransformFactory().createTransform(rijksdriehoek,wgs84);
	}
	
	/**
	 * @param rd_x x-coordinate in range of -7000  and 300 000
	 * @param rd_y y-coordinate in range of 289000 and 629 000
	 * @return LatLng class with doubles for latitude and longitude in WGS84 projection\nNull if RD-coordinates out of rangeb
	 */

	public Position toWGS84(int rd_x,int rd_y){
		if (rd_x < -7000 || rd_x > 300000){
			return null;
		}
		if (rd_y < 289000 || rd_y > 629000){
			return null;
		}
		ProjCoordinate src = new ProjCoordinate(rd_x,rd_y);
		ProjCoordinate tgt = new ProjCoordinate();
		factory.transform(src, tgt);
		Position.Builder builder = Position.newBuilder();
		builder.setLatitude((float)tgt.y);
		builder.setLongitude((float)tgt.x);
		return builder.build();
	}
}

@@ -287,16 +287,73 @@
 rdinate;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -577,16 +577,101 @@
 actory;%0A
+%09private static final Logger _log = LoggerFactory.getLogger(GeometryService.class);%0A%0A
 %0A%09public
@@ -1144,10 +1144,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09/**
@@ -1656,16 +1656,24 @@
 nate();%0A
+%09%09try%7B%0A%09
 %09%09factor
@@ -1695,16 +1695,17 @@
 , tgt);%0A
+%09
 %09%09Positi
@@ -1746,24 +1746,25 @@
 uilder();%0A%09%09
+%09
 builder.setL
@@ -1786,16 +1786,17 @@
 tgt.y);%0A
+%09
 %09%09builde
@@ -1827,16 +1827,17 @@
 t.x);%0A%09%09
+%09
 return b
@@ -1852,13 +1852,106 @@
 uild();%0A
+%09%09%7Dcatch(Exception e)%7B%0A%09%09%09_log.error(%22RD-%3EWGS84 x=%7B%7D,y=%7B%7D%22,rd_x,rd_y,e);%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Return null in case of proj4 crash and log