Permalink
Browse files

Heavy changes to the bulkdistricts API endpoint.

  • Loading branch information...
1 parent 746bc3e commit d08e2c8cfcb457ef1c650e60a0713323b83a8ac1 @GraylinKim GraylinKim committed Nov 8, 2012
@@ -129,13 +129,23 @@ private BluebirdAddress jsonToAddress(String id, JSONObject json) throws JSONExc
public STATUS status_code;
public String message;
public String address_id;
- public List<BOEAddressRange> matches;
-
- public Result(String id, STATUS status, String message, List<BOEAddressRange> matches) {
- this.address_id = id;
+ public int countyCode;
+ public int assemblyCode;
+ public int congressionalCode;
+ public int senateCode;
+ public int electionCode;
+ public String address;
+
+ public Result(STATUS status, String message, BluebirdAddress address, BOEAddressRange match) {
+ this.address_id = address.id;
this.status_code = status;
this.message = message;
- this.matches = matches;
+ this.address = address.toString();
+ this.electionCode = match.electionCode;
+ this.senateCode = match.senateCode;
+ this.congressionalCode = match.congressionalCode;
+ this.assemblyCode = match.assemblyCode;
+ this.countyCode = match.countyCode;
}
}
@@ -157,16 +167,38 @@ public Object execute(HttpServletRequest request, HttpServletResponse response,
for(BluebirdAddress address : bluebirdAddresses) {
if (address!=null) {
- List<BOEAddressRange> matches = streetData.getRanges(address);
- if (matches.size()==0) {
- results.add(new Result(address.id,Result.STATUS.NOMATCH,"No matches found for: "+address.toString(),matches));
- } else if (matches.size() > 1) {
- results.add(new Result(address.id,Result.STATUS.MULTIMATCH,matches.size()+" matches found for: "+address.toString(),matches));
+ List<BOEAddressRange> matches = streetData.getRanges(address,true);
+
+ if (matches.size()==1) {
+ results.add(new Result(Result.STATUS.MATCH,"EXACT MATCH",address,matches.get(0)));
+ continue;
+
+ } else if (matches.size()==0) {
+ // If at first you don't succeed, try again without the building number
+ matches = streetData.getRanges(address,false);
+
+ if (matches.size()==0) {
+ results.add(new Result(Result.STATUS.NOMATCH,"Street + Zip not Found for: "+address.toString(),address, new BOEAddressRange()));
+ } else {
+ // Consolidate these results to "range fill"
+ BOEAddressRange consolidated = AddressUtils.consolidateRanges(matches);
+ if (consolidated != null) {
+ results.add(new Result(Result.STATUS.MATCH,"CONSOLIDATED RANGEFILL",address, consolidated));
+ } else {
+ results.add(new Result(Result.STATUS.NOMATCH, "RANGEFILL failed for: "+address.toString(), address, new BOEAddressRange()));
+ }
+ }
} else {
- results.add(new Result(address.id,Result.STATUS.MATCH,"SUCCESS",matches));
+ BOEAddressRange consolidated = AddressUtils.consolidateRanges(matches);
+ if (consolidated != null) {
+ results.add(new Result(Result.STATUS.MATCH,"CONSOLIDATED MULTIMATCH",address, consolidated));
+ } else {
+ results.add(new Result(Result.STATUS.MULTIMATCH, matches.size()+" inconsistent matches found for: "+address.toString(), address, new BOEAddressRange()));
+ }
}
+
} else {
- results.add(new Result("-1",Result.STATUS.INVALID,"Invalid JSON Entry",noMatches));
+ results.add(new Result(Result.STATUS.INVALID,"Invalid JSON Entry",new BluebirdAddress("-1"),new BOEAddressRange()));
}
}
} catch (IOException e) {
@@ -1,6 +1,7 @@
package gov.nysenate.sage.boe;
import java.util.HashMap;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -82,6 +83,40 @@ public static BOEAddress normalizeAddress(BOEAddress address) {
return address;
}
+ public static BOEAddressRange consolidateRanges(List<BOEAddressRange> ranges) {
+ BOEAddressRange base = ranges.get(0);
+ int sd = base.senateCode;
+ int ed = base.electionCode;
+ int cong = base.congressionalCode;
+ int ad = base.assemblyCode;
+ int county = base.countyCode;
+ for (int i=1; i < ranges.size(); i++) {
+ BOEAddressRange range = ranges.get(i);
+ if (range.senateCode != sd) { sd = 0; }
+ if (range.electionCode != ed) { ed = 0; }
+ if (range.congressionalCode != cong) { cong = 0; }
+ if (range.assemblyCode != ad) { ad = 0; }
+ if (range.countyCode != county) { county = 0; }
+ }
+
+ if (sd != 0) {
+ BOEAddressRange range = new BOEAddressRange();
+ range.senateCode = sd;
+ range.assemblyCode = ad;
+ range.electionCode = ed;
+ range.congressionalCode = cong;
+ range.countyCode = county;
+ range.state = base.state;
+ range.street = base.street;
+ range.zip5 = base.zip5;
+ return range;
+
+ } else {
+ return null;
+ }
+
+ }
+
public static void load_constants() {
if (addrPattern!=null && suffixMap != null)
return;
@@ -66,7 +66,7 @@ public DistrictLookup(MysqlDataSource db) {
rangeHandler = new BeanListHandler<BOEAddressRange>(BOEAddressRange.class, new BasicRowProcessor(rowProcessor));
}
- public List<BOEAddressRange> getRanges(BOEStreetAddress address) throws SQLException {
+ public List<BOEAddressRange> getRanges(BOEStreetAddress address, boolean use_building) throws SQLException {
ArrayList<Object> a = new ArrayList<Object>();
String sql = "SELECT * \n"
+ "FROM street_data \n"
@@ -77,56 +77,64 @@ public DistrictLookup(MysqlDataSource db) {
a.add(address.zip5);
}
- if (!address.street.equals("")) {
+ if (!address.state.equals("")) {
+ sql += " AND state=? \n";
+ a.add(address.state);
+ }
- if (address.bldg_chr.equals("")) {
- if (address.apt_chr.equals("")) {
- sql += " AND (street=?) \n";
- a.add(address.street);
+ if (!address.street.equals("")) {
- } else {
- sql += " AND (street=? OR street=?)";
- a.add(address.street);
- a.add(address.street+" "+address.apt_chr);
- }
+ // Sometimes the bldg_chr is actually the tail end of the street name
+ if (!address.bldg_chr.equals("") && use_building==true) {
+// // Sometimes the apt_chr is actually the tail end of the street name
+// if (!address.apt_chr.equals("")) {
+// // TODO
+// } else {
+// // TODO
+// }
+ sql += " AND (street LIKE ? OR (street LIKE ? AND (bldg_lo_chr='' OR bldg_lo_chr <= ?) AND (bldg_hi_chr='' OR ? <= bldg_hi_chr))) \n";
+ a.add(address.bldg_chr+" "+address.street+"%");
+ a.add(address.street+"%");
+ a.add(address.bldg_chr);
+ a.add(address.bldg_chr);
} else {
- if (!address.apt_chr.equals("")) {
+ sql += " AND (street LIKE ?) \n";
+ a.add(address.street+"%");
+// // Sometimes the apt_chr is actually the tail end of the street name
+// if (address.apt_chr.equals("")) {
+// sql += " AND (street LIKE ?) \n";
+// a.add(address.street+"%");
+//
+// } else {
+// sql += " AND (street LIKE ? OR street LIKE ?)";
+// a.add(address.street+"%");
+// a.add(address.street+" "+address.apt_chr+"%");
+// }
- } else {
-
- }
- sql += " AND (street=? OR (street=? AND (bldg_lo_chr='' OR bldg_lo_chr <= ?) AND (bldg_hi_chr='' OR ? <= bldg_hi_chr))) \n";
- a.add(address.bldg_chr+" "+address.street);
- a.add(address.street);
- a.add(address.bldg_chr);
- a.add(address.bldg_chr);
}
- if (address.bldg_num != 0) {
+ if (address.bldg_num != 0 && use_building==true) {
sql += " AND (bldg_lo_num <= ? AND ? <= bldg_hi_num AND (bldg_parity='ALL' or bldg_parity=? )) \n";
a.add(address.bldg_num);
a.add(address.bldg_num);
a.add((address.bldg_num % 2 == 0 ? "EVENS" : "ODDS"));
}
}
- if (!address.town.equals("")) {
- sql += " AND town=? \n";
- a.add(address.town);
- }
- if (!address.state.equals("")) {
- sql += " AND state=? \n";
- a.add(address.state);
- }
if (DEBUG) {
System.out.println(sql);
for (Object o : a) {
System.out.println(o);
}
}
+
return runner.query(sql, rangeHandler, a.toArray());
}
+
+ public List<BOEAddressRange> getRanges(BOEStreetAddress address) throws SQLException {
+ return getRanges(address, true);
+ }
}
@@ -26,7 +26,7 @@ public static void main(String[] args) throws Exception {
ArrayList<StreetFile> street_files = new ArrayList<StreetFile>();
File base_dir = new File(config.get("directories", "street_files"));
- /*
+
street_files.add(new NTS(1, new File(base_dir, "Albany_County_Street_Index.txt")) {
@Override
public void store_extra_districts(BOEAddressRange addressRange, Matcher matcher) {
@@ -35,7 +35,7 @@ public void store_extra_districts(BOEAddressRange addressRange, Matcher matcher)
addressRange.setVillCode(matcher.group(17) != null ? matcher.group(17).trim() : "");
}
});
-
+ /*
street_files.add(new NTS(2, new File(base_dir, "Allegany_County_Streets.txt")) {
@Override
public void store_extra_districts(BOEAddressRange addressRange, Matcher matcher) {
@@ -438,16 +438,16 @@ public void store_extra_districts(AddressRange addressRange, Matcher matcher) {
// cor_code = (matcher.group(16) != null ? matcher.group(16).trim() : "");
}
});
-
+ */
// DELETED 1 incomplete line in the beginning of the file
street_files.add(new NTS(53,new File(base_dir, "Washington_County_Street_File.txt")) {
@Override
- public void store_extra_districts(AddressRange addressRange, Matcher matcher) {
+ public void store_extra_districts(BOEAddressRange addressRange, Matcher matcher) {
addressRange.setVillCode(matcher.group(15) != null ? matcher.group(15).trim() : "");
// jud_code = (matcher.group(16) != null ? matcher.group(16).trim() : "");
}
});
-
+ /*
street_files.add(new NTS(54,new File(base_dir, "Wayne_County_StreetFinder.txt")) {
@Override
public void store_extra_districts(AddressRange addressRange, Matcher matcher) {
@@ -466,10 +466,12 @@ public void store_extra_districts(AddressRange addressRange, Matcher matcher) {
}
});
*/
- StreetFile file = street_files.get(street_files.size()-1);
- System.out.println("Saving "+file.street_file.getAbsolutePath());
- file.clear(db);
- file.save(db);
+ for (StreetFile file : street_files) {
+ //StreetFile file = street_files.get(street_files.size()-1);
+ System.out.println("Saving "+file.street_file.getAbsolutePath());
+ file.clear(db);
+ file.save(db);
+ }
}
}

0 comments on commit d08e2c8

Please sign in to comment.