private void parseInput() {
    inputType = TYPE_UNKNOWN;
    int fcSrcSize = fcSrc.size();
    int fcDestSize = fcDest.size();

    // error - # geoms must match
    if (fcSrcSize != fcDestSize) {
      parseErrMsg = "Control point collections must be same size";
      return;
    }
    // for now only handling pair of geoms to define control points
    if (fcSrcSize != 1) {
      parseErrMsg = "Control points must be a single geometry";
      return;
    }

    geomSrc[0] = ((Feature) fcSrc.iterator().next()).getGeometry();
    geomDest[0] = ((Feature) fcDest.iterator().next()).getGeometry();

    if (geomSrc[0].getClass() != geomDest[0].getClass()) {
      parseErrMsg = "Control points must be LineStrings";
      return;
    }

    // for now only handling LineStrings
    if (!(geomSrc[0] instanceof LineString)) {
      parseErrMsg = "Control points must be LineStrings";
      return;
    }

    parseLines();
    return;
  }
Пример #2
0
  /**
   * Find the generic geometry type of the feature collection. Simple method - find the 1st non null
   * geometry and its type is the generic type. returns 0 - all empty/invalid <br>
   * 1 - point <br>
   * 2 - line <br>
   * 3 - polygon <br>
   *
   * @param fc feature collection containing tet geometries.
   */
  int findBestGeometryType(FeatureCollection fc) {
    Geometry geom;

    for (Iterator i = fc.iterator(); i.hasNext(); ) {
      geom = ((Feature) i.next()).getGeometry();

      if (geom instanceof Point) {
        return 1;
      }

      if (geom instanceof MultiPoint) {
        return 1;
      }

      if (geom instanceof Polygon) {
        return 3;
      }

      if (geom instanceof MultiPolygon) {
        return 3;
      }

      if (geom instanceof LineString) {
        return 2;
      }

      if (geom instanceof MultiLineString) {
        return 2;
      }
    }

    return 0;
  }
Пример #3
0
  /**
   * look at all the data in the column of the featurecollection, and find the largest string!
   *
   * @param fc features to look at
   * @param attributeNumber which of the column to test.
   */
  int findMaxStringLength(FeatureCollection fc, int attributeNumber) {
    int l;
    int maxlen = 0;
    Feature f;

    for (Iterator i = fc.iterator(); i.hasNext(); ) {
      f = (Feature) i.next();
      l = f.getString(attributeNumber).length();

      if (l > maxlen) {
        maxlen = l;
      }
    }

    return maxlen;
  }
Пример #4
0
  /**
   * Write a dbf file with the information from the featureCollection.
   *
   * @param featureCollection column data from collection
   * @param fname name of the dbf file to write to
   */
  void writeDbf(FeatureCollection featureCollection, String fname) throws Exception {
    DbfFileWriter dbf;
    FeatureSchema fs;
    int t;
    int f;
    int u;
    int num;

    fs = featureCollection.getFeatureSchema();

    // -1 because one of the columns is geometry
    DbfFieldDef[] fields = new DbfFieldDef[fs.getAttributeCount() - 1];

    // dbf column type and size
    f = 0;

    for (t = 0; t < fs.getAttributeCount(); t++) {
      AttributeType columnType = fs.getAttributeType(t);
      String columnName = fs.getAttributeName(t);

      if (columnType == AttributeType.INTEGER) {
        fields[f] = new DbfFieldDef(columnName, 'N', 16, 0);
        f++;
      } else if (columnType == AttributeType.DOUBLE) {
        fields[f] = new DbfFieldDef(columnName, 'N', 33, 16);
        f++;
      } else if (columnType == AttributeType.STRING) {
        int maxlength = findMaxStringLength(featureCollection, t);

        if (maxlength > 255) {
          throw new Exception(
              "ShapefileWriter does not support strings longer than 255 characters");
        }

        fields[f] = new DbfFieldDef(columnName, 'C', maxlength, 0);
        f++;
      } else if (columnType == AttributeType.DATE) {
        fields[f] = new DbfFieldDef(columnName, 'D', 8, 0);
        f++;
      } else if (columnType == AttributeType.GEOMETRY) {
        // do nothing - the .shp file handles this
      } else {
        throw new Exception("Shapewriter: unsupported AttributeType found in featurecollection.");
      }
    }

    // write header
    dbf = new DbfFileWriter(fname);
    dbf.writeHeader(fields, featureCollection.size());

    // write rows
    num = featureCollection.size();

    List features = featureCollection.getFeatures();

    for (t = 0; t < num; t++) {
      // System.out.println("dbf: record "+t);
      Feature feature = (Feature) features.get(t);
      Vector DBFrow = new Vector();

      // make data for each column in this feature (row)
      for (u = 0; u < fs.getAttributeCount(); u++) {
        AttributeType columnType = fs.getAttributeType(u);

        if (columnType == AttributeType.INTEGER) {
          Object a = feature.getAttribute(u);

          if (a == null) {
            DBFrow.add(new Integer(0));
          } else {
            DBFrow.add((Integer) a);
          }
        } else if (columnType == AttributeType.DOUBLE) {
          Object a = feature.getAttribute(u);

          if (a == null) {
            DBFrow.add(new Double(0.0));
          } else {
            DBFrow.add((Double) a);
          }
        } else if (columnType == AttributeType.DATE) {
          Object a = feature.getAttribute(u);
          if (a == null) {
            DBFrow.add("");
          } else {
            DBFrow.add(DbfFile.DATE_PARSER.format((Date) a));
          }
        } else if (columnType == AttributeType.STRING) {
          Object a = feature.getAttribute(u);

          if (a == null) {
            DBFrow.add(new String(""));
          } else {
            // MD 16 jan 03 - added some defensive programming
            if (a instanceof String) {
              DBFrow.add(a);
            } else {
              DBFrow.add(a.toString());
            }
          }
        }
      }

      dbf.writeRecord(DBFrow);
    }

    dbf.close();
  }
 public Iterator iterator() {
   return fc.iterator();
 }
 public List getFeatures() {
   return fc.getFeatures();
 }
 public boolean isEmpty() {
   return fc.isEmpty();
 }
 public void add(Feature feature) {
   fc.add(feature);
 }
 public Envelope getEnvelope() {
   return fc.getEnvelope();
 }
 public FeatureSchema getFeatureSchema() {
   return fc.getFeatureSchema();
 }
 public Collection remove(Envelope env) {
   return fc.remove(env);
 }
 public void removeAll(Collection features) {
   fc.removeAll(features);
 }
 public void addAll(Collection features) {
   fc.addAll(features);
 }
 public void remove(Feature feature) {
   fc.remove(feature);
 }
Пример #15
0
  /**
   * return a single geometry collection <br>
   * result.GeometryN(i) = the i-th feature in the FeatureCollection<br>
   * All the geometry types will be the same type (ie. all polygons) - or they will be set to<br>
   * NULL geometries<br>
   * <br>
   * GeometryN(i) = {Multipoint,Multilinestring, or Multipolygon)<br>
   *
   * @param fc feature collection to make homogeneous
   */
  public GeometryCollection makeSHAPEGeometryCollection(FeatureCollection fc) throws Exception {
    GeometryCollection result;
    Geometry[] allGeoms = new Geometry[fc.size()];

    int geomtype = findBestGeometryType(fc);

    if (geomtype == 0) {
      throw new Exception(
          "Could not determine shapefile type - data is either all GeometryCollections or empty");
    }

    List features = fc.getFeatures();

    for (int t = 0; t < features.size(); t++) {
      Geometry geom;
      geom = ((Feature) features.get(t)).getGeometry();

      switch (geomtype) {
        case 1: // point
          if ((geom instanceof Point)) {
            // good!
            Point[] p = new Point[1];
            p[0] = (Point) geom;

            allGeoms[t] = new MultiPoint(p, new PrecisionModel(), 0);
          } else if (geom instanceof MultiPoint) {
            allGeoms[t] = geom;
          } else {
            allGeoms[t] = new MultiPoint(null, new PrecisionModel(), 0);
          }

          break;

        case 2: // line
          if ((geom instanceof LineString)) {
            LineString[] l = new LineString[1];
            l[0] = (LineString) geom;

            allGeoms[t] = new MultiLineString(l, new PrecisionModel(), 0);
          } else if (geom instanceof MultiLineString) {
            allGeoms[t] = geom;
          } else {
            allGeoms[t] = new MultiLineString(null, new PrecisionModel(), 0);
          }

          break;

        case 3: // polygon
          if (geom instanceof Polygon) {
            // good!
            Polygon[] p = new Polygon[1];
            p[0] = (Polygon) geom;

            allGeoms[t] = makeGoodSHAPEMultiPolygon(new MultiPolygon(p, new PrecisionModel(), 0));
          } else if (geom instanceof MultiPolygon) {
            allGeoms[t] = makeGoodSHAPEMultiPolygon((MultiPolygon) geom);
          } else {
            allGeoms[t] = new MultiPolygon(null, new PrecisionModel(), 0);
          }

          break;
      }
    }

    result = new GeometryCollection(allGeoms, new PrecisionModel(), 0);

    return result;
  }
 public int size() {
   return fc.size();
 }
 public List query(Envelope envelope) {
   return fc.query(envelope);
 }