Java Code Examples for org.geotools.data.DataStore#createSchema()

The following examples show how to use org.geotools.data.DataStore#createSchema() . 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: GeoWaveGeoIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
private static SimpleFeatureType getStatementFeatureType(final DataStore dataStore) throws IOException, SchemaException {
    SimpleFeatureType featureType;

    final String[] datastoreFeatures = dataStore.getTypeNames();
    if (Arrays.asList(datastoreFeatures).contains(FEATURE_NAME)) {
        featureType = dataStore.getSchema(FEATURE_NAME);
    } else {
        featureType = DataUtilities.createType(FEATURE_NAME,
            SUBJECT_ATTRIBUTE + ":String," +
            PREDICATE_ATTRIBUTE + ":String," +
            OBJECT_ATTRIBUTE + ":String," +
            CONTEXT_ATTRIBUTE + ":String," +
            GEOMETRY_ATTRIBUTE + ":Geometry:srid=4326," +
            GEO_ID_ATTRIBUTE + ":String");

        dataStore.createSchema(featureType);
    }
    return featureType;
}
 
Example 2
Source File: GeoMesaGeoIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
private static SimpleFeatureType getStatementFeatureType(final DataStore dataStore) throws IOException, SchemaException {
    SimpleFeatureType featureType;

    final String[] datastoreFeatures = dataStore.getTypeNames();
    if (Arrays.asList(datastoreFeatures).contains(FEATURE_NAME)) {
        featureType = dataStore.getSchema(FEATURE_NAME);
    } else {
        final String featureSchema = SUBJECT_ATTRIBUTE + ":String," //
                + PREDICATE_ATTRIBUTE + ":String," //
                + OBJECT_ATTRIBUTE + ":String," //
                + CONTEXT_ATTRIBUTE + ":String," //
                + GEOMETRY_ATTRIBUTE + ":Geometry:srid=4326;geomesa.mixed.geometries='true'";
        featureType = SimpleFeatureTypes.createType(FEATURE_NAME, featureSchema);
        dataStore.createSchema(featureType);
    }
    return featureType;
}
 
Example 3
Source File: GeoWaveFeatureSourceTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
public void testEmpty() throws Exception {
  final SimpleFeatureType type =
      DataUtilities.createType(
          "GeoWaveFeatureSourceTest_e",
          "geometry:Geometry:srid=4326,pop:java.lang.Long,pid:String,when:Date");
  final DataStore dataStore = createDataStore();
  dataStore.createSchema(type);
  final SimpleFeatureSource source = dataStore.getFeatureSource("GeoWaveFeatureSourceTest_e");
  final ReferencedEnvelope env = source.getBounds();
  assertEquals(90.0, env.getMaxX(), 0.0001);
  assertEquals(-180.0, env.getMinY(), 0.0001);
  final Query query = new Query("GeoWaveFeatureSourceTest_e", Filter.INCLUDE);
  assertEquals(0, source.getCount(query));
}
 
Example 4
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 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 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 6
Source File: OmsShapefileFeatureWriter.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void writeFeatureCollection() throws IOException {
    if (!concatOr(!hasWritten, doReset)) {
        return;
    }

    pm.beginTask("Writing features to shapefile...", -1);

    if (!file.endsWith(".shp")) {
        file = file + ".shp";
    }
    if (geodata != null && geodata.size() != 0) {
        pType = geodata.getSchema();
    }
    File shapeFile = new File(file);
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");

    Map<String, Serializable> map = new HashMap<>();
    map.put("url", shapeFile.toURI().toURL());
    String shpDoIndex = PreferencesHandler.getShpDoIndex();
    if (shpDoIndex != null) {
        map.put("create spatial index", new Boolean(shpDoIndex));
    }

    String shpCharset = PreferencesHandler.getShpCharset();
    if (shpCharset != null) {
        map.put("charset", shpCharset);
    }

    DataStore newDataStore = factory.createNewDataStore(map);
    newDataStore.createSchema(pType);

    Transaction transaction = new DefaultTransaction("create");
    String typeName = newDataStore.getTypeNames()[0];
    SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

    featureStore.setTransaction(transaction);
    try {
        if (geodata == null) {
            featureStore.addFeatures(new DefaultFeatureCollection());
        } else {
            featureStore.addFeatures(geodata);
        }
        transaction.commit();
    } catch (Exception problem) {
        transaction.rollback();
        throw new IOException(problem.getLocalizedMessage());
    } finally {
        transaction.close();
        pm.done();
    }

    hasWritten = true;
}
 
Example 7
Source File: ShapefileQueryOutputFormat.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void output(final ResultSet results) {
  int geometryColumn = -1;
  for (int i = 0; i < results.columnCount(); i++) {
    if (Geometry.class.isAssignableFrom(results.columnType(i))) {
      geometryColumn = i;
      break;
    }
  }
  if (geometryColumn < 0) {
    throw new RuntimeException(
        "Unable to output results to a shapefile without a geometry column.");
  }

  final SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
  ftb.setCRS(results.getCRS());
  ftb.setName(typeName);
  for (int i = 0; i < results.columnCount(); i++) {
    final AttributeTypeBuilder atb = new AttributeTypeBuilder();
    atb.setBinding(results.columnType(i));
    atb.nillable(true);
    if (i == geometryColumn) {
      ftb.add(atb.buildDescriptor("the_geom"));
    } else {
      ftb.add(atb.buildDescriptor(results.columnName(i)));
    }
  }
  final SimpleFeatureType sft = ftb.buildFeatureType();

  final SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(sft);
  final AtomicLong nextId = new AtomicLong(0L);
  final Iterator<SimpleFeature> features = Iterators.transform(results, r -> {
    sfb.reset();
    for (int i = 0; i < results.columnCount(); i++) {
      sfb.add(r.columnValue(i));
    }
    final SimpleFeature feature = sfb.buildFeature(Long.toString(nextId.incrementAndGet()));
    return feature;
  });

  final FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
  final File file = new File(outputFile);
  final Map<String, Serializable> params = Maps.newHashMap();
  final Transaction transaction = new DefaultTransaction("Write Results");
  try {
    params.put("url", file.toURI().toURL());
    final DataStore dataStore = factory.createNewDataStore(params);
    dataStore.createSchema(sft);
    final SimpleFeatureStore store =
        (SimpleFeatureStore) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
    store.setTransaction(transaction);
    final SimpleFeatureCollection featureCollection =
        DataUtilities.collection(new SimpleFeatureIterator() {
          @Override
          public boolean hasNext() {
            return features.hasNext();
          }

          @Override
          public SimpleFeature next() throws NoSuchElementException {
            return features.next();
          }

          @Override
          public void close() {}
        });
    store.addFeatures(featureCollection);
    transaction.commit();
  } catch (final Exception e) {
    try {
      transaction.rollback();
    } catch (final IOException ioe) {
      throw new RuntimeException("Encountered an error when rolling back transaction", ioe);
    }
    throw new RuntimeException(
        "Encountered an error when writing the features to the file: " + e.getMessage(),
        e);
  }
}