Skip to content

Commit

Permalink
Merge pull request #9 from dkatzel-home/zipSupport
Browse files Browse the repository at this point in the history
Added support for reading zipped version of geonames files.
  • Loading branch information
AReallyGoodName committed Jul 12, 2015
2 parents d1087ac + 8190403 commit a57ee18
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/main/java/geocode/ReverseGeoCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ of this software and associated documentation files (the "Software"), to deal
package geocode;

import geocode.kdtree.KDTree;

import java.io.*;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
*
Expand All @@ -42,7 +45,39 @@ public class ReverseGeoCode {
KDTree<GeoName> kdTree;

// Get placenames from http://download.geonames.org/export/dump/
/**
* Parse the zipped geonames file.
* @param zippedPlacednames a {@link ZipInputStream} zip file downloaded from http://download.geonames.org/export/dump/; can not be null.
* @param majorOnly only include major cities in KD-tree.
*
* @throws IOException if there is a problem reading the {@link ZipInputStream}.
* @throws NullPointerException if zippedPlacenames is {@code null}.
*/
public ReverseGeoCode( ZipInputStream zippedPlacednames, boolean majorOnly ) throws IOException {
//depending on which zip file is given,
//country specific zip files have read me files
//that we should ignore
ZipEntry entry;
do{
entry = zippedPlacednames.getNextEntry();
}while(entry.getName().equals("readme.txt"));

createKdTree(zippedPlacednames, majorOnly);

}
/**
* Parse the raw text geonames file.
* @param placenames the text file downloaded from http://download.geonames.org/export/dump/; can not be null.
* @param majorOnly only include major cities in KD-tree.
*
* @throws IOException if there is a problem reading the stream.
* @throws NullPointerException if zippedPlacenames is {@code null}.
*/
public ReverseGeoCode( InputStream placenames, boolean majorOnly ) throws IOException {
createKdTree(placenames, majorOnly);
}
private void createKdTree(InputStream placenames, boolean majorOnly)
throws IOException {
ArrayList<GeoName> arPlaceNames;
arPlaceNames = new ArrayList<GeoName>();
// Read the geonames file in the directory
Expand All @@ -52,14 +87,14 @@ public ReverseGeoCode( InputStream placenames, boolean majorOnly ) throws IOExce
while ((str = in.readLine()) != null) {
GeoName newPlace = new GeoName(str);
if ( !majorOnly || newPlace.majorPlace ) {
arPlaceNames.add(new GeoName(str));
arPlaceNames.add(newPlace);
}
}
} catch (IOException ex) {
in.close();
throw ex;
}finally{
in.close();
}
in.close();
kdTree = new KDTree<GeoName>(arPlaceNames);
}

Expand Down

0 comments on commit a57ee18

Please sign in to comment.