1 package fr.ifremer.dali.map;
2
3 import fr.ifremer.dali.service.DaliTechnicalException;
4 import fr.ifremer.quadrige3.core.dao.technical.Assert;
5 import org.geotools.geometry.DirectPosition2D;
6 import org.geotools.geometry.jts.ReferencedEnvelope;
7 import org.geotools.referencing.CRS;
8 import org.opengis.referencing.FactoryException;
9 import org.opengis.referencing.crs.CoordinateReferenceSystem;
10 import org.opengis.referencing.operation.TransformException;
11
12 import java.io.IOException;
13 import java.net.URL;
14 import java.net.URLConnection;
15
16
17
18
19
20
21 public class Maps {
22
23 public static boolean isMapServerReachable(MapMode mapMode) {
24 Assert.notNull(mapMode);
25
26 if (!mapMode.isOnline()) return false;
27 try {
28 URL url = new URL(mapMode.getUrl());
29 URLConnection connection = url.openConnection();
30 connection.setConnectTimeout(1000);
31 connection.connect();
32 return true;
33 } catch (IOException e) {
34 return false;
35 }
36 }
37
38 public static ReferencedEnvelope transformReferencedEnvelope(ReferencedEnvelope envelope, CoordinateReferenceSystem crs) {
39
40 if (envelope != null && envelope.getCoordinateReferenceSystem() != null && crs != null) {
41 if (!CRS.equalsIgnoreMetadata(crs, envelope.getCoordinateReferenceSystem())) {
42 try {
43 return envelope.transform(crs, true);
44 } catch (TransformException | FactoryException e) {
45 throw new DaliTechnicalException(e);
46 }
47 }
48 }
49
50 return envelope;
51 }
52
53 public static DirectPosition2D transformDirectPosition(DirectPosition2D position, CoordinateReferenceSystem crs) {
54
55 if (position != null && crs != null) {
56 if (!CRS.equalsIgnoreMetadata(crs, position.getCoordinateReferenceSystem())) {
57 try {
58 ReferencedEnvelope envelope = new ReferencedEnvelope(position.x, position.x, position.y, position.y, position.getCoordinateReferenceSystem())
59 .transform(crs, true);
60 return new DirectPosition2D(envelope.getUpperCorner());
61 } catch (TransformException | FactoryException e) {
62 throw new DaliTechnicalException(e);
63 }
64 }
65 }
66 return position;
67 }
68
69 }