org.geotools.data.FeatureWriter Java Examples

The following examples show how to use org.geotools.data.FeatureWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveFeature() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            ECQL.toFilter("pid like '" + pids.get(
                    0).substring(
                    0,
                    1) + "%'"),
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);

    // Remove
    final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
        dataStore.getFeatureWriter(type.getTypeName(), Transaction.AUTO_COMMIT);
    try {
        while (writer.hasNext()) {
            writer.next();
            writer.remove();
        }
    } finally {
        writer.close();
    }

    // Re-query
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader2 =
            dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int recount = 0;
    while (reader2.hasNext()) {
        reader2.next();
        recount++;
    }
    assertEquals(0, recount);
}
 
Example #2
Source File: GeoWaveFeatureSource.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
protected FeatureWriter<SimpleFeatureType, SimpleFeature> getWriterInternal(
    final Query query,
    final int flags) throws IOException {
  final GeoWaveTransactionState state = getDataStore().getMyTransactionState(transaction, this);
  return new GeoWaveFeatureWriter(
      components,
      state.getGeoWaveTransaction(query.getTypeName()),
      (GeoWaveFeatureReader) getReaderInternal(query));
}
 
Example #3
Source File: WFSBoundedSpatialQueryTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
public void populate() throws IOException, CQLException, ParseException {
  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();
  writer.close();
  transaction1.commit();
  transaction1.close();
}
 
Example #4
Source File: WFSSpatialTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws IOException, CQLException, ParseException {
  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T18:33:55Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:33:55Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  writer.write();
  writer.close();

  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, transaction1);
  assertTrue(reader.hasNext());
  final SimpleFeature priorFeature = reader.next();
  assertEquals(newFeature.getAttribute("pid"), priorFeature.getAttribute("pid"));
  assertFalse(reader.hasNext());
  reader.close();

  transaction1.commit();
  transaction1.close();
}
 
Example #5
Source File: GeoWaveFeatureSourceTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void populate(final SimpleFeatureType type, final DataStore dataStore)
    throws IOException, CQLException, ParseException {

  dataStore.createSchema(type);

  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(77));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(66));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.242)));
  writer.write();
  writer.close();
  transaction1.commit();
  transaction1.close();
}
 
Example #6
Source File: GeoWaveFeatureSourceTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void populate(final SimpleFeatureType type, final DataStore dataStore)
    throws IOException, CQLException, ParseException {

  dataStore.createSchema(type);

  final Transaction transaction1 = new DefaultTransaction();

  final SimpleFeatureStore source =
      (SimpleFeatureStore) dataStore.getFeatureSource(type.getName());
  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(77));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  source.addFeatures(DataUtilities.collection(newFeature));

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(66));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
  source.addFeatures(DataUtilities.collection(newFeature));

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.242)));
  source.addFeatures(DataUtilities.collection(newFeature));
  transaction1.commit();
  transaction1.close();
}
 
Example #7
Source File: WFSBoundedQueryTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
public void populate() throws IOException, CQLException, ParseException {
  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();
  writer.close();
  transaction1.commit();
  transaction1.close();
}
 
Example #8
Source File: GeoToolsAttributesSubsetTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws IOException, GeoWavePluginException, SchemaException {
  geotoolsDataStore = createDataStore();
  type = DataUtilities.createType(typeName, typeSpec);

  geotoolsDataStore.createSchema(type);
  final Transaction transaction = new DefaultTransaction();
  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      geotoolsDataStore.getFeatureWriter(type.getTypeName(), transaction);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute(
      geometry_attribute,
      GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(41.25, 41.25)));
  newFeature.setAttribute(long_attribute, 1l);
  newFeature.setAttribute(string_attribute, "string1");
  writer.write();
  newFeature = writer.next();
  newFeature.setAttribute(
      geometry_attribute,
      GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(41.5, 41.5)));
  newFeature.setAttribute(long_attribute, 2l);
  newFeature.setAttribute(string_attribute, "string2");
  writer.write();
  newFeature = writer.next();
  newFeature.setAttribute(
      geometry_attribute,
      GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(41.75, 41.75)));
  newFeature.setAttribute(long_attribute, 3l);
  newFeature.setAttribute(string_attribute, "string3");
  writer.write();
  writer.close();
  transaction.commit();
  transaction.close();
}
 
Example #9
Source File: WFSTemporalQueryTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
public void populate() throws IOException, CQLException, ParseException {
  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("start", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
  newFeature.setAttribute("end", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("start", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
  newFeature.setAttribute("end", DateUtilities.parseISO("2005-05-20T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
  writer.write();

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("start", DateUtilities.parseISO("2005-05-21T20:32:56Z"));
  newFeature.setAttribute("end", DateUtilities.parseISO("2005-05-22T20:32:56Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
  writer.write();
  writer.close();
  transaction1.commit();
  transaction1.close();
}
 
Example #10
Source File: TemporalRangeTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws ParseException, IOException {
  final Calendar gmt = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  final Calendar local = Calendar.getInstance(TimeZone.getTimeZone("EDT"));
  local.setTimeInMillis(gmt.getTimeInMillis());
  final TemporalRange rGmt = new TemporalRange(gmt.getTime(), gmt.getTime());
  final TemporalRange rLocal = new TemporalRange(local.getTime(), local.getTime());
  rGmt.fromBinary(rGmt.toBinary());
  assertEquals(gmt.getTime(), rGmt.getEndTime());
  assertEquals(rLocal.getEndTime(), rGmt.getEndTime());
  assertEquals(rLocal.getEndTime().getTime(), rGmt.getEndTime().getTime());

  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  final SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(77));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T19:32:56-04:00"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));

  final FeatureTimeRangeStatistics stats = new FeatureTimeRangeStatistics(null, "when");
  stats.entryIngested(newFeature);

  assertEquals(
      DateUtilities.parseISO("2005-05-19T23:32:56Z"),
      stats.asTemporalRange().getStartTime());
}
 
Example #11
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws SchemaException, CQLException, Exception {
    setupConf();
    try (final GeoWaveGeoIndexer indexer = new GeoWaveGeoIndexer()) {
        indexer.setConf(conf);
        dataStore = indexer.getGeoToolsDataStore();
        // Clear old data
        indexer.purge(conf);

        type = DataUtilities.createType(
                "GeoWaveFeatureReaderTest",
                "geometry:Geometry:srid=4326,start:Date,end:Date,pop:java.lang.Long,pid:String");

        dataStore.createSchema(type);

        stime = DateUtilities.parseISO("2005-05-15T20:32:56Z");
        etime = DateUtilities.parseISO("2005-05-20T20:32:56Z");

        final Transaction transaction1 = new DefaultTransaction();
        final FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriter(
                type.getTypeName(),
                transaction1);
        assertFalse(writer.hasNext());
        SimpleFeature newFeature = writer.next();
        newFeature.setAttribute(
                "pop",
                Long.valueOf(100));
        newFeature.setAttribute(
                "pid",
                "a" + UUID.randomUUID().toString());
        newFeature.setAttribute(
                "start",
                stime);
        newFeature.setAttribute(
                "end",
                etime);
        newFeature.setAttribute(
                "geometry",
                factory.createPoint(new Coordinate(27.25, 41.25)));
        fids.add(newFeature.getID());
        pids.add(newFeature.getAttribute("pid").toString());
        writer.write();
        newFeature = writer.next();
        newFeature.setAttribute(
                "pop",
                Long.valueOf(101));
        newFeature.setAttribute(
                "pid",
                "b" + UUID.randomUUID().toString());
        newFeature.setAttribute(
                "start",
                etime);
        newFeature.setAttribute(
                "geometry",
                factory.createPoint(new Coordinate(28.25, 41.25)));
        fids.add(newFeature.getID());
        pids.add(newFeature.getAttribute("pid").toString());
        writer.write();
        writer.close();
        transaction1.commit();
        transaction1.close();

        query = new Query(
                "GeoWaveFeatureReaderTest",
                ECQL.toFilter("IN ('" + fids.get(0) + "')"),
                new String[] {
                    "geometry",
                    "pid"
                });
    }
}
 
Example #12
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws SchemaException, CQLException, Exception {
  dataStore = createDataStore();
  type =
      DataUtilities.createType(
          "GeoWaveFeatureReaderTest",
          "geometry:Geometry:srid=4326,start:Date,end:Date,pop:java.lang.Long,pid:String");
  ((GeoWaveGTDataStore) dataStore).getIndexStore().addIndex(
      new SpatialIndexBuilder().createIndex());
  ((GeoWaveGTDataStore) dataStore).getIndexStore().addIndex(
      new SpatialTemporalIndexBuilder().createIndex());
  dataStore.createSchema(type);

  stime = DateUtilities.parseISO("2005-05-15T20:32:56Z");
  mtime = DateUtilities.parseISO("2005-05-20T20:32:56Z");
  etime = DateUtilities.parseISO("2005-05-25T20:32:56Z");

  final Transaction transaction1 = new DefaultTransaction();
  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", "a" + UUID.randomUUID().toString());
  newFeature.setAttribute("start", stime);
  newFeature.setAttribute("end", mtime);
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  fids.add(newFeature.getID());
  pids.add(newFeature.getAttribute("pid").toString());
  writer.write();
  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(101));
  newFeature.setAttribute("pid", "b" + UUID.randomUUID().toString());
  newFeature.setAttribute("start", mtime);
  newFeature.setAttribute("end", etime);
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(28.25, 41.25)));
  fids.add(newFeature.getID());
  pids.add(newFeature.getAttribute("pid").toString());
  writer.write();
  writer.close();
  transaction1.commit();
  transaction1.close();

  query =
      new Query(
          "GeoWaveFeatureReaderTest",
          ECQL.toFilter("IN ('" + fids.get(0) + "')"),
          new String[] {"geometry", "pid"});
}
 
Example #13
Source File: WFSTransactionTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Test
public void testInsertIsolation() throws IOException, CQLException {
  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  final SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  writer.write();
  writer.close();

  FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, transaction1);
  assertTrue(reader.hasNext());
  final SimpleFeature priorFeature = reader.next();
  assertEquals(newFeature.getAttribute("pid"), priorFeature.getAttribute("pid"));
  reader.close();

  // uncommitted at this point, so this next transaction should not see
  // it.

  final Transaction transaction2 = new DefaultTransaction();
  reader = dataStore.getFeatureReader(query, transaction2);
  assertFalse(reader.hasNext());
  reader.close();

  transaction1.commit();
  reader = dataStore.getFeatureReader(query, transaction1);
  assertTrue(reader.hasNext());
  reader.next();
  assertFalse(reader.hasNext());
  reader.close();

  transaction1.close();

  // since this implementation does not support serializable, transaction2
  // can see the changes even though
  // it started after transaction1 and before the commit.
  reader = dataStore.getFeatureReader(query, transaction2);
  assertTrue(reader.hasNext());
  reader.next();
  assertFalse(reader.hasNext());
  reader.close();
  transaction2.commit();
  transaction2.close();

  // stats check
  final Transaction transaction3 = new DefaultTransaction();
  reader =
      ((GeoWaveFeatureSource) ((GeoWaveGTDataStore) dataStore).getFeatureSource(
          "geostuff",
          transaction3)).getReaderInternal(query);
  final Map<StatisticsId, InternalDataStatistics<SimpleFeature, ?, ?>> transStats =
      ((GeoWaveFeatureReader) reader).getTransaction().getDataStatistics();
  assertNotNull(
      transStats.get(
          FeatureNumericRangeStatistics.STATS_TYPE.newBuilder().fieldName(
              "pop").build().getId()));
  transaction3.close();
}
 
Example #14
Source File: ShapefileTool.java    From geowave with Apache License 2.0 4 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
    value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE",
    justification = "Directories may alreadybe there")
public static void writeShape(final String typeName, final File dir, final Geometry[] shapes)
    throws IOException {

  FileUtils.deleteDirectory(dir);

  dir.mkdirs();

  final SimpleFeatureBuilder featureBuilder =
      new SimpleFeatureBuilder(createFeatureType(typeName, shapes[0] instanceof Point));

  final ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

  final Map<String, Serializable> params = new HashMap<>();
  params.put("url", new File(dir.getAbsolutePath() + "/" + typeName + ".shp").toURI().toURL());
  params.put("create spatial index", Boolean.TRUE);

  final ShapefileDataStore newDataStore =
      (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
  newDataStore.createSchema(createFeatureType(typeName, shapes[0] instanceof Point));
  final Transaction transaction = new DefaultTransaction("create");

  try (final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      newDataStore.getFeatureWriterAppend(typeName, transaction)) {
    final int i = 1;
    for (final Geometry shape : shapes) {
      featureBuilder.add(shape);
      featureBuilder.add(Integer.valueOf(i));
      final SimpleFeature feature = featureBuilder.buildFeature(null);
      final SimpleFeature copy = writer.next();
      for (final AttributeDescriptor attrD : feature.getFeatureType().getAttributeDescriptors()) {
        // the null case should only happen for geometry
        if (copy.getFeatureType().getDescriptor(attrD.getName()) != null) {
          copy.setAttribute(attrD.getName(), feature.getAttribute(attrD.getName()));
        }
      }
      // shape files force geometry name to be 'the_geom'. So isolate
      // this change
      copy.setDefaultGeometry(feature.getDefaultGeometry());
      writer.write();
    }
  } catch (final IOException e) {
    LOGGER.warn("Problem with the FeatureWritter", e);
    transaction.rollback();
  } finally {
    transaction.commit();
    transaction.close();
  }
}
 
Example #15
Source File: ShapefileTool.java    From geowave with Apache License 2.0 4 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
    value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE",
    justification = "Directories may alreadybe there")
public static void writeShape(final File dir, final List<SimpleFeature> shapes)
    throws IOException {

  FileUtils.deleteDirectory(dir);

  dir.mkdirs();

  final ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
  final String typeName = shapes.get(0).getType().getTypeName();
  final Map<String, Serializable> params = new HashMap<>();
  params.put("url", new File(dir.getAbsolutePath() + "/" + typeName + ".shp").toURI().toURL());
  params.put("create spatial index", Boolean.TRUE);

  final ShapefileDataStore newDataStore =
      (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
  newDataStore.createSchema(shapes.get(0).getFeatureType());
  final Transaction transaction = new DefaultTransaction("create");

  try (final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      newDataStore.getFeatureWriterAppend(typeName, transaction)) {
    for (final SimpleFeature shape : shapes) {
      final SimpleFeature copy = writer.next();
      for (final AttributeDescriptor attrD : copy.getFeatureType().getAttributeDescriptors()) {
        // the null case should only happen for geometry
        if (copy.getFeatureType().getDescriptor(attrD.getName()) != null) {
          copy.setAttribute(attrD.getName(), shape.getAttribute(attrD.getName()));
        }
      }
      // shape files force geometry name to be 'the_geom'. So isolate
      // this change
      copy.setDefaultGeometry(shape.getDefaultGeometry());
      writer.write();
    }
  } catch (final IOException e) {
    LOGGER.warn("Problem with the FeatureWritter", e);
    transaction.rollback();
  } finally {
    transaction.commit();
    transaction.close();
  }
}