Beispiel #1
0
 private static void saveJMLfile(FeatureCollection fc, String filename) throws Exception {
   JMLWriter writer = new JMLWriter();
   DriverProperties dp = new DriverProperties();
   dp.set("File", filename);
   writer.write(fc, dp);
 }
  /**
   * Main method - write the featurecollection to a shapefile (2d, 3d or 4d).
   *
   * @param featureCollection collection to write
   * @param dp 'OutputFile' or 'DefaultValue' to specify where to write, and 'ShapeType' to specify
   *     dimentionality.
   */
  public void write(FeatureCollection featureCollection, DriverProperties dp)
      throws IllegalParametersException, Exception {
    String shpfileName;
    String dbffname;
    String shxfname;

    String path;
    String fname;
    String fname_withoutextention;
    int shapeType;
    int loc;

    GeometryCollection gc;

    shpfileName = dp.getProperty(FILE_PROPERTY_KEY);

    if (shpfileName == null) {
      shpfileName = dp.getProperty(DEFAULT_VALUE_PROPERTY_KEY);
    }

    if (shpfileName == null) {
      throw new IllegalParametersException("no output filename specified");
    }

    loc = shpfileName.lastIndexOf(File.separatorChar);

    if (loc == -1) {
      // loc = 0; // no path - ie. "hills.shp"
      // path = "";
      // fname = shpfileName;
      // probably using the wrong path separator character.
      throw new Exception(
          "couldn't find the path separator character '"
              + File.separatorChar
              + "' in your shape file name. This you're probably using the unix (or dos) one.");
    } else {
      path = shpfileName.substring(0, loc + 1); // ie. "/data1/hills.shp" -> "/data1/"
      fname = shpfileName.substring(loc + 1); // ie. "/data1/hills.shp" -> "hills.shp"
    }

    loc = fname.lastIndexOf(".");

    if (loc == -1) {
      throw new IllegalParametersException("Filename must end in '.shp'");
    }

    fname_withoutextention = fname.substring(0, loc); // ie. "hills.shp" -> "hills."
    dbffname = path + fname_withoutextention + ".dbf";

    writeDbf(featureCollection, dbffname);

    // this gc will be a collection of either multi-points, multi-polygons, or multi-linestrings
    // polygons will have the rings in the correct order
    gc = makeSHAPEGeometryCollection(featureCollection);

    shapeType = 2; // x,y

    if (dp.getProperty(SHAPE_TYPE_PROPERTY_KEY) != null) {
      String st = dp.getProperty(SHAPE_TYPE_PROPERTY_KEY);

      if (st.equalsIgnoreCase("xy")) {
        shapeType = 2;
      } else if (st.equalsIgnoreCase("xym")) {
        shapeType = 3;
      } else if (st.equalsIgnoreCase("xymz")) {
        shapeType = 4;
      } else if (st.equalsIgnoreCase("xyzm")) {
        shapeType = 4;
      } else if (st.equalsIgnoreCase("xyz")) {
        shapeType = 4;
      } else {
        throw new IllegalParametersException(
            "ShapefileWriter.write() - dataproperties has a 'ShapeType' that isnt 'xy', 'xym', or 'xymz'");
      }
    } else {
      if (gc.getNumGeometries() > 0) {
        shapeType = guessCoorinateDims(gc.getGeometryN(0));
      }
    }

    URL url = new URL("file", "localhost", shpfileName);
    Shapefile myshape = new Shapefile(url);
    myshape.write(gc, shapeType);

    shxfname = path + fname_withoutextention + ".shx";

    BufferedOutputStream in = new BufferedOutputStream(new FileOutputStream(shxfname));
    EndianDataOutputStream sfile = new EndianDataOutputStream(in);

    myshape.writeIndex(gc, sfile, shapeType);
  }