From: Wladimir S. <bo...@gm...> - 2008-06-27 13:14:18
|
Hi, I have a GPX file. I use GEOTOOLS to transform the wpt form EPSG:4326 to ESPG:23031. My problem is not getting the desired results. For example: For the point <wpt lat="41.5549967" lon="1.6694312"> the result must be X:389132.033 Y:4601425.53. but i obtain this result X:389036.16739 Y:4601396.25356. Where is the problem ??? There is my code. import org.opengis.referencing.FactoryException; import org.opengis.referencing.crs.*; import org.opengis.referencing.operation.*; import org.opengis.spatialschema.geometry.DirectPosition; import org.opengis.spatialschema.geometry.MismatchedDimensionException; import org.geotools.geometry.GeneralDirectPosition; import org.geotools.referencing.CRS; import org.geotools.referencing.FactoryFinder; public class TransCoord { TransCoord(){ try { //getTranformEPSG("234","23031","4326",389132.033,4601425.53); getTranformEPSG("234","4326","23031",1.6694312,41.5549967); } catch (MismatchedDimensionException e) { e.printStackTrace(); } catch (FactoryException e) { e.printStackTrace(); } catch (TransformException e) { e.printStackTrace(); } } public double[] getTranformEPSG(java.lang.String key,String inEpsg,String outEpsg,double coordX,double coordY) throws FactoryException, MismatchedDimensionException, TransformException{ double[]punt=new double[2]; double[]puntOut=new double[2]; String inEpsgStr=""; String outEpsgStr=""; //Se puede enviar el codigo EPSG con o sin EPSG: if(inEpsg.indexOf("EPSG")!=-1){inEpsgStr=inEpsg;}else{inEpsgStr="EPSG:"+inEpsg;} if(outEpsg.indexOf("EPSG")!=-1){outEpsgStr=outEpsg;}else{outEpsgStr="EPSG:"+outEpsg;} //Geotools 2.3 con el True devuelve la X primero CoordinateReferenceSystem sourceCRS = CRS.decode(inEpsgStr,true); CoordinateReferenceSystem targetCRS = null; targetCRS = CRS.decode(outEpsgStr,true); //System.out.println("---------------------"); //System.out.println("Sistema:"+"("+outEpsgStr+")"+targetCRS.toWKT().toString()); //System.out.println("---------------------"); //Geotools 2.3 con el True no da error de Datum Bursa Wolf //MathTransform mathTransform =CRS.findMathTransform( sourceCRS, targetCRS,true ); MathTransform mathTransform =CRS.findMathTransform(sourceCRS, targetCRS, true); DirectPosition pt = new GeneralDirectPosition(coordX, coordY); //x,y,z pt = mathTransform.transform(pt, null); punt = pt.getCoordinates(); puntOut[0]=Math.floor(punt[0]*100000)/100000; puntOut[1]=Math.floor(punt[1]*100000)/100000; //puntOut[0]=punt[0]; //puntOut[1]=punt[1]; System.out.println("X:"+puntOut[0]+" Y:" + puntOut[1]); //longitude,latitude return puntOut; } public static void main(String[] args) { new TransCoord(); } } |