1 |
package dk.daoas.naermestepshop; |
2 |
|
3 |
import geocode.GeoPoint; |
4 |
import geocode.kdtree.KDTree; |
5 |
|
6 |
import java.io.File; |
7 |
import java.io.FileReader; |
8 |
import java.util.List; |
9 |
import java.util.Properties; |
10 |
import java.util.logging.Logger; |
11 |
|
12 |
public class PshopMain { |
13 |
|
14 |
static Logger logger = Logger.getLogger(PshopMain.class.getName()); |
15 |
static final String CONFIG_FILENAME = "naermestepshop.properties"; |
16 |
|
17 |
|
18 |
public static void main(String[] args) throws Exception { |
19 |
|
20 |
File confFile = new File( CONFIG_FILENAME ); |
21 |
if (! confFile.exists() ) { |
22 |
logger.warning("Config file not found: " + CONFIG_FILENAME); |
23 |
System.exit(1); |
24 |
} |
25 |
|
26 |
Properties props = new Properties(); |
27 |
props.load( new FileReader(confFile) ); |
28 |
|
29 |
|
30 |
Database db = new Database(props); |
31 |
|
32 |
List<GeoPoint> dao = (List<GeoPoint>) db.getShops("DAO"); |
33 |
List<GeoPoint> swip = (List<GeoPoint>) db.getShops("SWIP"); |
34 |
List<GeoPoint> gls = (List<GeoPoint>) db.getShops("GLS"); |
35 |
|
36 |
KDTree<GeoPoint> daoTree = new KDTree<GeoPoint>(dao); |
37 |
|
38 |
KDTree<GeoPoint> swipTree = new KDTree<GeoPoint>(swip); |
39 |
KDTree<GeoPoint> glsTree = new KDTree<GeoPoint>(gls); |
40 |
|
41 |
logger.info("Start fetching addr"); |
42 |
|
43 |
List<Adresse> adresser = db.getAdresser(); |
44 |
|
45 |
db.resetResultTable();//Nulstil resultat tabellen inden vi starter beregningen |
46 |
|
47 |
logger.info("Start calculating"); |
48 |
for (Adresse addr : adresser) { |
49 |
ShopResult daoResult = new ShopResult(); |
50 |
daoResult.shop = (Shop) daoTree.findNearest(addr); |
51 |
daoResult.distance = GeoPointHelper.beregnAfstand(addr, daoResult.shop); |
52 |
|
53 |
ShopResult swipResult = new ShopResult(); |
54 |
swipResult.shop = (Shop) swipTree.findNearest(addr); |
55 |
swipResult.distance = GeoPointHelper.beregnAfstand(addr, swipResult.shop); |
56 |
|
57 |
ShopResult glsResult = new ShopResult(); |
58 |
glsResult.shop = (Shop) glsTree.findNearest(addr); |
59 |
glsResult.distance = GeoPointHelper.beregnAfstand(addr, glsResult.shop); |
60 |
|
61 |
if ( Double.isNaN(daoResult.distance) || Double.isNaN(swipResult.distance) || Double.isNaN(glsResult.distance)) { |
62 |
System.out.println("Addr " + addr); |
63 |
System.out.println("DAO " + daoResult); |
64 |
System.out.println("Swip " + swipResult); |
65 |
System.out.println("GLS " + glsResult); |
66 |
} |
67 |
|
68 |
db.gemResultat(addr, daoResult, swipResult, glsResult); |
69 |
} |
70 |
|
71 |
db.saveBatch(); |
72 |
|
73 |
logger.info("done"); |
74 |
|
75 |
} |
76 |
|
77 |
/* |
78 |
|
79 |
@Deprecated |
80 |
public static GeoPoint bruteforceSearch(GeoPoint needle, List<GeoPoint> haystack) { |
81 |
GeoPoint nearest = null; |
82 |
double bestDistance = 999999.999; |
83 |
for (GeoPoint current : haystack) { |
84 |
double distance = GeoPointHelper.beregnAfstand(current, needle); |
85 |
|
86 |
if (distance < bestDistance) { |
87 |
nearest = current; |
88 |
bestDistance = distance; |
89 |
} |
90 |
} |
91 |
|
92 |
|
93 |
return nearest; |
94 |
} |
95 |
|
96 |
|
97 |
/*Shop nearest2 = (Shop) bruteforceSearch(addr, dao); |
98 |
if (nearest1.ktonr != nearest2.ktonr) { |
99 |
System.out.println("Mistmatch " + addr + " " + nearest1 + " / " + nearest2); |
100 |
} |
101 |
|
102 |
|
103 |
GeoPoint reference = new GeoPoint(55.8727, 9.88129); |
104 |
|
105 |
final int MAX = 10000; |
106 |
|
107 |
TimingHelper t1 = new TimingHelper(); |
108 |
for (int i = 0; i<MAX; i++) { |
109 |
daoTree.findNearest(reference); |
110 |
} |
111 |
t1.printElapsed("Tree "); |
112 |
|
113 |
TimingHelper t2 = new TimingHelper(); |
114 |
for (int i = 0; i<MAX; i++) { |
115 |
bruteforceSearch(reference, dao); |
116 |
} |
117 |
t2.printElapsed("Brute "); |
118 |
*/ |
119 |
} |