001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.sandbox.flickr.geo;
031
032import java.awt.RenderingHints;
033import java.io.BufferedReader;
034import java.io.DataInput;
035import java.io.DataOutput;
036import java.io.File;
037import java.io.FileReader;
038import java.io.IOException;
039import java.io.PrintWriter;
040import java.util.ArrayList;
041import java.util.List;
042import java.util.Scanner;
043import java.util.regex.Pattern;
044
045import org.jfree.chart.ChartPanel;
046import org.jfree.chart.JFreeChart;
047import org.jfree.chart.axis.NumberAxis;
048import org.jfree.chart.plot.FastScatterPlot;
049import org.jfree.ui.ApplicationFrame;
050import org.openimaj.math.geometry.point.Coordinate;
051
052public class PlotFlickrGeo {
053        static class ImgRec implements Coordinate {
054                public static final Pattern csvregex = Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
055
056                String farm;
057                String server;
058                String id;
059                String secret;
060                String originalSecret;
061                String mediumUrl;
062                String imageDir;
063                String title;
064                String description;
065                String license;
066                String datePosted;
067                String dateTaken;
068                String ownerid;
069                String username;
070                String accuracy;
071                double latitude;
072                double longitude;
073                String tags;
074
075                @Override
076                public Number getOrdinate(int dimension) {
077                        if (dimension == 0)
078                                return latitude;
079                        return longitude;
080                }
081
082                @Override
083                public void setOrdinate(int dimension, Number value) {
084                        if (dimension == 0)
085                                latitude = value.doubleValue();
086                        if (dimension == 1)
087                                longitude = value.doubleValue();
088                }
089
090                @Override
091                public int getDimensions() {
092                        return 2;
093                }
094
095                public float getOrdinateValue(int dimension) {
096                        if (dimension == 0)
097                                return (float) latitude;
098                        return (float) longitude;
099                }
100
101                public static ImgRec makeImgRec(String s) {
102                        final String[] parts = csvregex.split(s);
103
104                        if (parts.length == 19) {
105                                if (parts[15].trim() != "null" && parts[16].trim() != "null") {
106                                        final ImgRec rec = new ImgRec();
107                                        rec.farm = parts[0].trim();
108                                        rec.server = parts[1].trim();
109                                        rec.id = parts[2].trim();
110                                        rec.secret = parts[3].trim();
111                                        rec.originalSecret = parts[4].trim();
112                                        rec.mediumUrl = parts[5].trim();
113                                        rec.imageDir = parts[6].trim();
114                                        rec.title = parts[7].trim();
115                                        rec.description = parts[8].trim();
116                                        rec.license = parts[9].trim();
117                                        rec.datePosted = parts[10].trim();
118                                        rec.dateTaken = parts[11].trim();
119                                        rec.ownerid = parts[12].trim();
120                                        rec.username = parts[13].trim();
121                                        rec.accuracy = parts[14].trim();
122                                        if (parts[15].trim().length() == 0)
123                                                return null;
124                                        rec.latitude = Double.parseDouble(parts[15].trim());
125                                        if (parts[16].trim().length() == 0)
126                                                return null;
127                                        rec.longitude = Double.parseDouble(parts[16].trim());
128                                        // rec.tags = parts[17].trim();
129
130                                        return rec;
131                                }
132                        }
133                        return null;
134                }
135
136                @Override
137                public void readASCII(Scanner in) throws IOException {
138                }
139
140                @Override
141                public String asciiHeader() {
142                        return null;
143                }
144
145                @Override
146                public void readBinary(DataInput in) throws IOException {
147                }
148
149                @Override
150                public byte[] binaryHeader() {
151                        return null;
152                }
153
154                @Override
155                public void writeASCII(PrintWriter out) throws IOException {
156                }
157
158                @Override
159                public void writeBinary(DataOutput out) throws IOException {
160                }
161
162                @Override
163                public String toString() {
164                        String csv = "";
165                        csv += String.format("%s, ", farm);
166                        csv += String.format("%s, ", server);
167                        csv += String.format("%s, ", id);
168                        csv += String.format("%s, ", secret);
169                        csv += String.format("%s, ", originalSecret);
170                        csv += String.format("%s, ", mediumUrl);
171                        csv += String.format("%s, ", imageDir);
172                        csv += String.format("%s, ", title);
173                        csv += String.format("%s, ", description);
174                        csv += String.format("%s, ", license);
175                        csv += String.format("%s, ", datePosted);
176                        csv += String.format("%s, ", dateTaken);
177                        csv += String.format("%s, ", ownerid);
178                        csv += String.format("%s, ", username);
179                        csv += String.format("%s, ", accuracy);
180                        csv += String.format("%f, ", latitude);
181                        csv += String.format("%f, ", longitude);
182                        csv += String.format("%s, ", tags);
183                        csv += "\n";
184                        return csv;
185                }
186        }
187
188        // public static void main(String[] args) throws IOException {
189        // File inputcsv = new
190        // File("/Volumes/Raid/FlickrCrawls/AllGeo16/images.csv");
191        //
192        // int size = 10000000;
193        // List<float[]> data = new ArrayList<float[]>(size);
194        // //read in images
195        // BufferedReader br = new BufferedReader(new FileReader(inputcsv));
196        // String line;
197        // int i = 0;
198        // while ((line = br.readLine()) != null) {
199        // //ImgRec rec = ImgRec.makeImgRec(line);
200        //
201        // String [] parts = ImgRec.csvregex.split(line);
202        //
203        // if (parts.length == 19) {
204        // String p15 = parts[15].trim();
205        // String p16 = parts[16].trim();
206        //
207        // if (p15 != "null" && p16 != "null" && p15.length()>0 && p16.length()>0) {
208        // float latitude = Float.parseFloat(p15);
209        // float longitude = Float.parseFloat(p16);
210        //
211        // data.add(new float[] {longitude, latitude});
212        // }
213        // }
214        //
215        // if (i%10000 == 0) System.out.println(i);
216        // if (i++ > size) break;
217        // }
218        //
219        // System.out.println("Done reading");
220        //
221        // float[][] dataArr = new float[2][data.size()];
222        // for (i=0; i<data.size(); i++) {
223        // dataArr[0][i] = data.get(i)[0];
224        // dataArr[1][i] = data.get(i)[1];
225        // }
226        //
227        // NumberAxis domainAxis = new NumberAxis("X");
228        // domainAxis.setRange(-180, 180);
229        // NumberAxis rangeAxis = new NumberAxis("Y");
230        // rangeAxis.setRange(-90, 90);
231        // FastScatterPlot plot = new FastScatterPlot(dataArr, domainAxis,
232        // rangeAxis);
233        //
234        // JFreeChart chart = new JFreeChart("Fast Scatter Plot", plot);
235        // chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING,
236        // RenderingHints.VALUE_ANTIALIAS_ON);
237        //
238        // ChartPanel chartPanel = new ChartPanel(chart);
239        // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
240        // final ApplicationFrame frame = new ApplicationFrame("Title");
241        // frame.setContentPane(chartPanel);
242        // frame.pack();
243        // frame.setVisible(true);
244        // }
245
246        // public static void main(String[] args) throws IOException {
247        // File inputcsv = new
248        // File("/Volumes/Raid/FlickrCrawls/AllGeo16/images.csv");
249        // File outputcsv = new File("/Users/jsh2/Desktop/world-geo.csv");
250        //
251        // BufferedWriter bw = new BufferedWriter(new FileWriter(outputcsv));
252        //
253        // //read in images
254        // BufferedReader br = new BufferedReader(new FileReader(inputcsv));
255        // String line;
256        // int i = 0;
257        // while ((line = br.readLine()) != null) {
258        // String [] parts = ImgRec.csvregex.split(line);
259        //
260        // if (parts.length == 19) {
261        // String p15 = parts[15].trim();
262        // String p16 = parts[16].trim();
263        //
264        // if (p15 != "null" && p16 != "null" && p15.length()>0 && p16.length()>0) {
265        // float latitude = Float.parseFloat(p15);
266        // float longitude = Float.parseFloat(p16);
267        //
268        // bw.write(String.format("%f, %f\n", longitude, latitude));
269        // }
270        // }
271        //
272        // if (i++%10000 == 0) System.out.println(i);
273        // }
274        // }
275
276        // public static void main(String[] args) throws IOException {
277        // final File inputcsv = new File("/Users/jsh2/Desktop/world-geo.csv");
278        // final List<float[]> data = new ArrayList<float[]>(10000000);
279        //
280        // // read in images
281        // final BufferedReader br = new BufferedReader(new FileReader(inputcsv));
282        // String line;
283        // int i = 0;
284        // while ((line = br.readLine()) != null) {
285        // final String[] parts = line.split(",");
286        //
287        // final float longitude = Float.parseFloat(parts[0]);
288        // final float latitude = Float.parseFloat(parts[1]);
289        //
290        // data.add(new float[] { longitude, latitude });
291        //
292        // if (i++ % 10000 == 0)
293        // System.out.println(i);
294        // }
295        // br.close();
296        //
297        // System.out.println("Done reading");
298        //
299        // final float[][] dataArr = new float[2][data.size()];
300        // for (i = 0; i < data.size(); i++) {
301        // dataArr[0][i] = data.get(i)[0];
302        // dataArr[1][i] = data.get(i)[1];
303        // }
304        //
305        // final NumberAxis domainAxis = new NumberAxis("X");
306        // domainAxis.setRange(-180, 180);
307        // final NumberAxis rangeAxis = new NumberAxis("Y");
308        // rangeAxis.setRange(-90, 90);
309        // final FastScatterPlot plot = new FastScatterPlot(dataArr, domainAxis,
310        // rangeAxis);
311        //
312        // final JFreeChart chart = new JFreeChart("Fast Scatter Plot", plot);
313        // chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING,
314        // RenderingHints.VALUE_ANTIALIAS_ON);
315        //
316        // final ChartPanel chartPanel = new ChartPanel(chart);
317        // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
318        // final ApplicationFrame frame = new ApplicationFrame("Title");
319        // frame.setContentPane(chartPanel);
320        // frame.pack();
321        // frame.setVisible(true);
322        // }
323
324        public static void main(String[] args) throws IOException {
325                final File inputcsv = new File("/Volumes/SSD/training_latlng");
326                final List<double[]> data = new ArrayList<double[]>(10000000);
327
328                // read in images
329                final BufferedReader br = new BufferedReader(new FileReader(inputcsv));
330                String line;
331                int i = 0;
332                br.readLine();
333                while ((line = br.readLine()) != null) {
334                        final String[] parts = line.split(" ");
335
336                        final double longitude = Double.parseDouble(parts[2]);
337                        final double latitude = Double.parseDouble(parts[1]);
338
339                        data.add(new double[] { longitude, latitude });
340
341                        if (longitude >= -0.1 && longitude < 0 && latitude > 50 && latitude < 54)
342                                System.out.println(parts[0] + " " + latitude + " " + longitude);
343
344                        // if (i++ % 10000 == 0)
345                        // System.out.println(i);
346                }
347                br.close();
348
349                System.out.println("Done reading");
350
351                final float[][] dataArr = new float[2][data.size()];
352                for (i = 0; i < data.size(); i++) {
353                        dataArr[0][i] = (float) data.get(i)[0];
354                        dataArr[1][i] = (float) data.get(i)[1];
355                }
356
357                final NumberAxis domainAxis = new NumberAxis("X");
358                domainAxis.setRange(-180, 180);
359                final NumberAxis rangeAxis = new NumberAxis("Y");
360                rangeAxis.setRange(-90, 90);
361                final FastScatterPlot plot = new FastScatterPlot(dataArr, domainAxis, rangeAxis);
362
363                final JFreeChart chart = new JFreeChart("Fast Scatter Plot", plot);
364                chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
365
366                final ChartPanel chartPanel = new ChartPanel(chart);
367                chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
368                final ApplicationFrame frame = new ApplicationFrame("Title");
369                frame.setContentPane(chartPanel);
370                frame.pack();
371                frame.setVisible(true);
372        }
373
374}