コード例 #1
0
ファイル: KmlReader.java プロジェクト: hamiltont/Empower
  /**
   * @param inputFile
   * @return true if the file was read and parsed properly, false otherwise
   */
  public boolean read(File inputFile) {
    KMLParser parser = new KMLParser();
    Configuration.properties.setProperty(Configuration.GENERATE_IDS, Configuration.OFF);

    Kml kml;

    GeoLocation topRight = null;
    GeoLocation bottomLeft = null;

    try {
      kml = parser.parse(inputFile);
      Document d = kml.getDocument();
      Feature[] features = d.getFeatures();

      mPoly = new ArrayList<MyPolygon>(features.length);

      for (Feature f : features) {
        Placemark p = (Placemark) f;

        double[] coords =
            p.getPolygon().getOuterBoundaryIs().getLinearRing().getNumericalCoordinates();

        MyPolygon poly = new MyPolygon();
        ArrayList<GeoLocation> polyPoints =
            new ArrayList<GeoLocation>(Math.round((float) coords.length * 0.666666f));

        for (int i = 0; i < coords.length; i = i + 3) {

          GeoLocation dp = new GeoLocation();
          dp.lon = coords[i];
          dp.lat = coords[i + 1];

          if (topRight == null) {
            topRight = new GeoLocation(dp.lat, dp.lon);
            bottomLeft = new GeoLocation(dp.lat, dp.lon);
          }

          if (dp.lat > topRight.lat) topRight.lat = dp.lat;
          if (dp.lat < bottomLeft.lat) bottomLeft.lat = dp.lat;

          if (dp.lon > topRight.lon) topRight.lon = dp.lon;
          if (dp.lon < bottomLeft.lon) bottomLeft.lon = dp.lon;

          polyPoints.add(dp);
        }

        poly.mLocations = polyPoints;
        mPoly.add(poly);
      }

    } catch (IOException e) {
      e.printStackTrace();
      return false;
    } catch (SAXException e) {
      e.printStackTrace();
      return false;
    }

    double margin = .5d;

    // Add Horiz Margins
    bottomLeft.lon -= margin;
    topRight.lon += margin;

    // Add Vert margins
    bottomLeft.lat -= margin;
    topRight.lat += margin;

    mTopRight = topRight;
    mBottomLeft = bottomLeft;

    return true;
  }