Java Code Examples for org.geotools.feature.AttributeTypeBuilder#setBinding()

The following examples show how to use org.geotools.feature.AttributeTypeBuilder#setBinding() . 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: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
private void addDateWithFormatToFeatureType(String format) {
    SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
    typeBuilder.init(featureType);

    AttributeDescriptor dateAtt;
    AttributeTypeBuilder dateAttBuilder = new AttributeTypeBuilder();
    dateAttBuilder.setName("dateAttrWithFormat");
    dateAttBuilder.setBinding(Date.class);
    dateAtt = dateAttBuilder.buildDescriptor("dateAttrWithFormat", dateAttBuilder.buildType());
    dateAtt.getUserData().put(DATE_FORMAT, format);
    typeBuilder.add(dateAtt);

    featureType = typeBuilder.buildFeatureType();
    setFilterBuilder();
}
 
Example 2
Source File: FeatureExtender.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param oldFeatureType the {@link FeatureType} of the existing features.
 * @param fieldArray the list of the names of new fields. 
 * @param classesArray the list of classes of the new fields.
 * @throws FactoryRegistryException 
 * @throws SchemaException
 */
public FeatureExtender( SimpleFeatureType oldFeatureType, String[] fieldArray,
        Class<?>[] classesArray ) throws FactoryRegistryException, SchemaException {

    List<AttributeDescriptor> oldAttributeDescriptors = oldFeatureType
            .getAttributeDescriptors();
    List<AttributeDescriptor> addedAttributeDescriptors = new ArrayList<AttributeDescriptor>();
    for( int i = 0; i < fieldArray.length; i++ ) {
        AttributeTypeBuilder build = new AttributeTypeBuilder();
        build.setNillable(true);
        build.setBinding(classesArray[i]);
        AttributeDescriptor descriptor = build.buildDescriptor(fieldArray[i]);
        addedAttributeDescriptors.add(descriptor);
    }

    List<AttributeDescriptor> newAttributesTypesList = new ArrayList<AttributeDescriptor>();
    for( AttributeDescriptor attributeDescriptor : oldAttributeDescriptors ) {
        newAttributesTypesList.add(attributeDescriptor);
    }
    newAttributesTypesList.addAll(addedAttributeDescriptors);

    // create the feature type
    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
    b.setName(oldFeatureType.getName());
    b.setCRS(oldFeatureType.getCoordinateReferenceSystem());
    b.addAll(newAttributesTypesList);
    newFeatureType = b.buildFeatureType();
}
 
Example 3
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 4 votes vote down vote up
@Before
public void setUp() {
    ff = CommonFactoryFinder.getFilterFactory2();

    SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
    typeBuilder.setName("test");
    typeBuilder.add("stringAttr", String.class);
    typeBuilder.add("integerAttr", Integer.class);
    typeBuilder.add("longAttr", Long.class);
    typeBuilder.add("booleanAttr", Boolean.class);
    typeBuilder.add("doubleAttr", Double.class);
    typeBuilder.add("floatAttr", Float.class);
    typeBuilder.add("dateAttr", Date.class);

    AttributeDescriptor geoPointAtt;
    AttributeTypeBuilder geoPointAttBuilder = new AttributeTypeBuilder();
    geoPointAttBuilder.setName("geo_point");
    geoPointAttBuilder.setBinding(Point.class);
    geoPointAtt = geoPointAttBuilder.buildDescriptor("geo_point", geoPointAttBuilder.buildType());
    geoPointAtt.getUserData().put(GEOMETRY_TYPE, ElasticGeometryType.GEO_POINT);
    typeBuilder.add(geoPointAtt);

    AttributeDescriptor geoShapeAtt;
    AttributeTypeBuilder geoShapeAttBuilder = new AttributeTypeBuilder();
    geoShapeAttBuilder.setName("geom");
    geoShapeAttBuilder.setBinding(Geometry.class);
    geoShapeAtt = geoShapeAttBuilder.buildDescriptor("geom", geoShapeAttBuilder.buildType());
    geoShapeAtt.getUserData().put(GEOMETRY_TYPE, ElasticGeometryType.GEO_SHAPE);
    typeBuilder.add(geoShapeAtt);

    AttributeDescriptor analyzedAtt;
    AttributeTypeBuilder analyzedAttBuilder = new AttributeTypeBuilder();
    analyzedAttBuilder.setName("analyzed");
    analyzedAttBuilder.setBinding(String.class);
    analyzedAtt = analyzedAttBuilder.buildDescriptor("analyzed", analyzedAttBuilder.buildType());
    analyzedAtt.getUserData().put(ANALYZED, true);
    typeBuilder.add(analyzedAtt);

    AttributeDescriptor netsedAtt;
    AttributeTypeBuilder nestedAttBuilder = new AttributeTypeBuilder();
    nestedAttBuilder.setName("nested.hej");
    nestedAttBuilder.setBinding(String.class);
    netsedAtt = nestedAttBuilder.buildDescriptor("nested.hej", nestedAttBuilder.buildType());
    netsedAtt.getUserData().put(NESTED, true);
    netsedAtt.getUserData().put(ANALYZED, true);
    typeBuilder.add(netsedAtt);

    AttributeDescriptor netsedDateAtt;
    AttributeTypeBuilder nestedDateAttBuilder = new AttributeTypeBuilder();
    nestedDateAttBuilder.setName("nested.datehej");
    nestedDateAttBuilder.setBinding(Date.class);
    netsedDateAtt = nestedDateAttBuilder.buildDescriptor("nested.datehej", nestedDateAttBuilder.buildType());
    netsedDateAtt.getUserData().put(NESTED, true);
    typeBuilder.add(netsedDateAtt);

    featureType = typeBuilder.buildFeatureType();
    setFilterBuilder();

    parameters = new HashMap<>();
    final Hints hints = new Hints();
    hints.put(Hints.VIRTUAL_TABLE_PARAMETERS, parameters);
    query = new Query();
    query.setHints(hints);

    dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    gf = new GeometryFactory();
}
 
Example 4
Source File: GeoJsonQueryOutputFormat.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void output(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 geojson without a geometry column.");
  }

  SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
  ftb.setName(typeName);
  ftb.setCRS(results.getCRS());
  for (int i = 0; i < results.columnCount(); i++) {
    AttributeTypeBuilder atb = new AttributeTypeBuilder();
    atb.setBinding(results.columnType(i));
    atb.nillable(true);
    ftb.add(atb.buildDescriptor(results.columnName(i)));
  }
  SimpleFeatureType sft = ftb.buildFeatureType();
  final SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(sft);
  final AtomicLong nextId = new AtomicLong(0L);
  Iterator<SimpleFeature> features = Iterators.transform(results, r -> {
    sfb.reset();
    for (int i = 0; i < results.columnCount(); i++) {
      sfb.add(r.columnValue(i));
    }
    SimpleFeature feature = sfb.buildFeature(Long.toString(nextId.incrementAndGet()));
    return feature;
  });


  try {
    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() {}
        });
    FeatureJSON io = new FeatureJSON();
    io.writeFeatureCollection(featureCollection, outputFile);
  } catch (IOException e) {
    throw new RuntimeException(
        "Encountered exception when writing geojson file: " + e.getMessage(),
        e);
  }
}
 
Example 5
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);
  }
}