Пример #1
0
  /** ** Main entery point for debugging/testing */
  public static void main(String argv[]) {
    RTConfig.setCommandLineArgs(argv);
    Print.setAllOutputToStdout(true);
    Print.setEncoding(ENCODING_UTF8);
    String accountID = RTConfig.getString(ARG_ACCOUNT, "demo");
    GoogleGeocodeV2 gn = new GoogleGeocodeV2("google", null, null);

    /* reverse geocode */
    if (RTConfig.hasProperty(ARG_REVGEOCODE)) {
      GeoPoint gp = new GeoPoint(RTConfig.getString(ARG_REVGEOCODE, null));
      if (!gp.isValid()) {
        Print.logInfo("Invalid GeoPoint specified");
        System.exit(1);
      }
      Print.logInfo("Reverse-Geocoding GeoPoint: " + gp);
      Print.sysPrintln(
          "RevGeocode = " + gn.getReverseGeocode(gp, null /*localeStr*/, false /*cache*/));
      // Note: Even though the values are printed in UTF-8 character encoding, the
      // characters may not appear to be property displayed if the console display
      // does not support UTF-8.
      System.exit(0);
    }

    /* no options */
    Print.sysPrintln("No options specified");
    System.exit(1);
  }
Пример #2
0
  /**
   * ** Returns true if the parent records in their respective parent tables exist. ** @return True
   * if the parent records exist.
   */
  public boolean parentsExist() throws DBException {
    DBFactory<gDBR> dbFact = this.getFactory();
    DBFieldValues myFldVals = this.getFieldValues();
    java.util.List<String> parentList = dbFact.getParentTables();
    for (String parentTable : parentList) {

      /* get parent table DBFactory */
      Print.logInfo("[%s] Parent table: %s", this.getTableName(), parentTable);
      DBFactory parentFact = DBFactory.getFactoryByName(parentTable);
      if (parentFact == null) {
        Print.logError("Unexpected error finding parent table: " + parentTable);
        return false;
      }

      /* create parent record key with fields from this key */
      DBRecordKey parentKey = parentFact.createKey(); // an empty key
      DBField parentKeyFlds[] = parentFact.getKeyFields();
      for (DBField pkf : parentKeyFlds) {
        String pfn = pkf.getName();

        /* get this DBField */
        DBField myKeyFld = this.getField(pfn);
        if (myKeyFld == null) {
          Print.logError("Unexpected error finding field: [" + this.getTableName() + "] " + pfn);
          return false;
        }

        /* get parent key field value */
        Object pkv = myFldVals.getFieldValue(pfn);
        if (pkv == null) {
          Print.logError("Unexpected error finding parent field: [" + parentTable + "] " + pfn);
          return false;
        }
        if (myKeyFld.isDefaultValue(pkv)) {
          Print.logInfo("This key contains a global value, skipping parent check: " + parentTable);
          parentKey = null;
          break;
        }
        parentKey.setFieldValue(pfn, pkv);
      }

      /* check parent existence */
      if ((parentKey != null) && !parentKey.exists()) {
        Print.logError("Parent record does not exist: [" + parentTable + "] " + parentKey);
        return false;
      }
    }
    return true;
  }
Пример #3
0
  /** ** Main entery point for debugging/testing */
  public static void main(String argv[]) {
    RTConfig.setCommandLineArgs(argv);
    Print.setAllOutputToStdout(true);
    Print.setEncoding(ENCODING_UTF8);

    /* host */
    String host = RTConfig.getString("host", null);
    if (!StringTools.isBlank(host)) {
      HOST_PRIMARY = host;
    }

    /* GeoPoint */
    GeoPoint gp = new GeoPoint(RTConfig.getString("gp", null));
    if (!gp.isValid()) {
      Print.logInfo("Invalid GeoPoint specified");
      System.exit(1);
    }
    Print.logInfo("Reverse-Geocoding GeoPoint: " + gp);

    /* Reverse Geocoding */
    Nominatim gn = new Nominatim("nominatim", null, RTConfig.getCommandLineProperties());
    Print.sysPrintln("RevGeocode = " + gn.getReverseGeocode(gp, null /*localeStr*/));
  }
Пример #4
0
  /**
   * ** Returns a ReverseGeocode instance containing address information ** @param gp The GeoPoint
   * ** @return The ReverseGeocode instance
   */
  private ReverseGeocode getAddressReverseGeocode(GeoPoint gp, String localeStr) {

    /* URL */
    String url = this.getAddressReverseGeocodeURL(gp);
    Print.logInfo("Address URL: " + url);

    /* create XML document */
    Document xmlDoc = GetXMLDocument(url);
    if (xmlDoc == null) {
      return null;
    }

    /* create ReverseGeocode response */
    Element reversegeocode = xmlDoc.getDocumentElement();
    if (!reversegeocode.getTagName().equalsIgnoreCase(TAG_reversegeocode)) {
      return null;
    }

    /* init */
    String address_val = null; // null address
    String house_val = null; // house number
    String road_val = null; // street name
    String city_val = null; // city name
    String county_val = null; // county name
    String state_val = null; // state/province
    String postcode_val = null; // postal code
    String country_name_val = null; // country name
    String country_code_val = null; // country code

    // full address
    NodeList resultList = XMLTools.getChildElements(reversegeocode, TAG_result);
    for (int r = 0; r < resultList.getLength(); r++) {
      Element result = (Element) resultList.item(r);
      // String osmType = XMLTools.getAttribute(result, ATTR_osm_type, null, false);
      address_val = XMLTools.getNodeText(result, " ", false);
      break; // only the first element
    }

    // address components
    NodeList addresspartsList = XMLTools.getChildElements(reversegeocode, TAG_addressparts);
    for (int a = 0; (a < addresspartsList.getLength()); a++) {
      Element addressparts = (Element) addresspartsList.item(a);
      NodeList addresspartsChildren = addressparts.getChildNodes();
      for (int ac = 0; ac < addresspartsChildren.getLength(); ac++) {
        Node child = addresspartsChildren.item(ac);
        if (!(child instanceof Element)) {
          continue;
        }
        Element elem = (Element) child;
        String elemName = elem.getNodeName();
        if (elemName.equalsIgnoreCase(TAG_house)) {
          house_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_tram)) {
          // ignore
        } else if (elemName.equalsIgnoreCase(TAG_road)) {
          road_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_residential)) {
          // ignore
        } else if (elemName.equalsIgnoreCase(TAG_village)) {
          // ignore
        } else if (elemName.equalsIgnoreCase(TAG_town)) {
          if (StringTools.isBlank(city_val)) {
            city_val = XMLTools.getNodeText(elem, " ", false);
          }
        } else if (elemName.equalsIgnoreCase(TAG_city)) {
          city_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_county)) {
          county_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_postcode)) {
          postcode_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_state)) {
          state_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_country)) {
          country_name_val = XMLTools.getNodeText(elem, " ", false);
        } else if (elemName.equalsIgnoreCase(TAG_country_code)) {
          country_code_val = StringTools.trim(XMLTools.getNodeText(elem, " ", false)).toUpperCase();
        } else {
          // elemName unrecognized
        }
      }
      break; // only the first element
    }

    /* populate ReverseGeocode instance */
    ReverseGeocode rg = new ReverseGeocode();
    StringBuffer addr = new StringBuffer();
    // house number /road
    if (!StringTools.isBlank(house_val)) {
      addr.append(house_val);
      if (!StringTools.isBlank(road_val)) {
        addr.append(" ");
        addr.append(road_val);
        rg.setStreetAddress(house_val + " " + road_val);
      } else {
        rg.setStreetAddress(house_val);
      }
    } else if (!StringTools.isBlank(road_val)) {
      addr.append(road_val);
      rg.setStreetAddress(road_val);
    }
    // city/county
    if (!StringTools.isBlank(city_val)) {
      if (addr.length() > 0) {
        addr.append(", ");
      }
      addr.append(city_val);
      rg.setCity(city_val);
    }
    if (!StringTools.isBlank(county_val)) {
      if (StringTools.isBlank(city_val)) {
        // "city" not provided, at least include the "county"
        if (addr.length() > 0) {
          addr.append(", ");
        }
        addr.append("[").append(county_val).append("]");
      }
      // rg.setCounty(county_val);
    }
    // state/province
    if (!StringTools.isBlank(state_val)) {
      if (addr.length() > 0) {
        addr.append(", ");
      }
      addr.append(state_val);
      rg.setStateProvince(state_val);
      if (!StringTools.isBlank(postcode_val)) {
        addr.append(" ").append(postcode_val);
        rg.setPostalCode(postcode_val);
      }
    } else {
      if (!StringTools.isBlank(postcode_val)) {
        if (addr.length() > 0) {
          addr.append(", ");
        }
        addr.append(postcode_val);
        rg.setPostalCode(postcode_val);
      }
    }
    // country
    if (!StringTools.isBlank(country_code_val)) {
      if (country_code_val.equalsIgnoreCase("US")) {
        // if (addr.length() > 0) { addr.append(", "); }
        // addr.append("USA");
      } else if (!StringTools.isBlank(country_name_val)) {
        if (addr.length() > 0) {
          addr.append(", ");
        }
        addr.append(country_name_val);
      } else {
        if (addr.length() > 0) {
          addr.append(", ");
        }
        addr.append(country_code_val);
      }
      rg.setCountryCode(country_code_val);
    }
    // full address
    rg.setFullAddress(addr.toString());

    return rg;
  }