@Override
 public void setDefaultGeometry(Object newGeometry) {
   Property p = getDefaultGeometryProperty();
   if (p != null) {
     p.setValue(newGeometry);
   }
 }
 @Override
 public void setAttribute(int index, Object newValue) throws IndexOutOfBoundsException {
   Property p = getProperties().get(index);
   if (p != null) {
     p.setValue(newValue);
   }
 }
 @Override
 public void setAttribute(Name eName, Object newValue) {
   Property p = getProperty(eName);
   if (p != null) {
     p.setValue(newValue);
   }
 }
  /**
   * Helper method for subclasses to reduce null checks
   *
   * @param expression
   * @param object
   * @return value or null
   */
  protected Object eval(org.opengis.filter.expression.Expression expression, Object object) {
    if (expression == null) return null;
    Object value = expression.evaluate(object);

    // NC - HACK (similar to below), but for complex features
    if (value instanceof org.opengis.feature.ComplexAttribute) {
      Property simpleContent =
          ((org.opengis.feature.ComplexAttribute) value).getProperty(new NameImpl("simpleContent"));
      if (simpleContent == null) {
        value = null;
      } else {
        value = simpleContent.getValue();
      }
    }

    // HACK as this method is used internally for filter
    // evaluation comparisons, etc, they work over the
    // contents (i.e. comparing an attexpresion with a literal)
    // so, lacking a better way of doing so, I'm putting this
    // check here
    if (value instanceof org.opengis.feature.Attribute) {
      value = ((org.opengis.feature.Attribute) value).getValue();
    }
    return value;
  }
 @Override
 public void setValue(Collection<Property> values) {
   for (Property it : values) {
     EAttribute eAttr = eStructure().eGetAttribute(it.getName().getURI());
     eFeature().eSet(eAttr, it.getValue());
   }
 }
 /**
  * Return the simple content of a {@link ComplexAttribute} if it represents a complexType with
  * simpleContent, otherwise null.
  *
  * @param complex
  * @return
  */
 public static Object getSimpleContent(ComplexAttribute complex) {
   Property simpleContent = complex.getProperty(new NameImpl("simpleContent"));
   if (simpleContent == null) {
     return null;
   } else {
     return simpleContent.getValue();
   }
 }
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    String networkPath = args[0];
    String newNetworkPath = args[1];
    String shapeFilePath = args[2];

    // String shapeFile = "C:/Work/Roadpricing Scenarios/SiouxFalls/Network/SiouxFalls_nodes.shp";

    Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());

    new NetworkReaderMatsimV1(scenario).parse(networkPath);
    Network network = scenario.getNetwork();
    Map<Id<Node>, ? extends Node> nodes = network.getNodes();

    Double x = 0.0;
    Double y = 0.0;

    System.out.println(shapeFilePath);
    ShapeFileReader shapeFileReader = new ShapeFileReader();
    Collection<SimpleFeature> fts = shapeFileReader.readFileAndInitialize(shapeFilePath);
    log.info("Shape file contains " + fts.size() + " features!");

    for (SimpleFeature ft : fts) {
      Geometry geo = (Geometry) ft.getDefaultGeometry();
      Coordinate[] coordinates = geo.getCoordinates();
      Collection<Property> properties = ft.getProperties("Id");

      System.out.println(
          "Feature: "
              + ft.getID()
              + ","
              + ft.getIdentifier().toString()
              + ","
              + ft.getName().toString());

      for (int i = 0; i < coordinates.length; i++) {
        System.out.print(coordinates[i].x + "," + coordinates[i].y + "   ");
        x = coordinates[i].x;
        y = coordinates[i].y;
      }

      for (Property p : properties) {
        System.out.println("Value: " + p.getValue().toString());
        Node node = nodes.get(Id.create(p.getValue().toString(), Node.class));
        node.getCoord().setXY(x, y);
        System.out.println("Name: " + p.getName().toString());
        System.out.println("Descriptor: " + p.getDescriptor().toString());
      }
      System.out.println();
      System.out.println();

      NetworkWriter writer = new NetworkWriter(network);
      writer.write(newNetworkPath);
    }
  }
 @Override
 public Collection<Property> getProperties(Name eName) {
   Set<Property> eItems = new HashSet<Property>();
   for (Property it : getProperties()) {
     if (it.getName().equals(eName)) {
       eItems.add(it);
     }
   }
   return eItems;
 }
Esempio n. 9
0
  @Test
  public void testImageReaderPolyphemusSimple() throws Exception {
    final File file = TestData.file(this, "O3-NO2.nc");
    final NetCDFImageReaderSpi unidataImageReaderSpi = new NetCDFImageReaderSpi();
    assertTrue(unidataImageReaderSpi.canDecodeInput(file));
    NetCDFImageReader reader = null;
    try {

      // checking low level
      reader = (NetCDFImageReader) unidataImageReaderSpi.createReaderInstance();
      reader.setInput(file);
      int numImages = reader.getNumImages(true);
      LOGGER.info("Found " + numImages + " images.");
      for (int i = 0; i < numImages; i++) {
        Slice2DIndex sliceIndex = reader.getSlice2DIndex(i);
        assertNotNull(sliceIndex);
        spitOutSliceInformation(i, sliceIndex);
      }

      // checking slice catalog
      final CoverageSlicesCatalog cs = reader.getCatalog();
      assertNotNull(cs);

      // get typenames
      final String[] typeNames = cs.getTypeNames();
      for (String typeName : typeNames) {
        final List<CoverageSlice> granules = cs.getGranules(new Query(typeName, Filter.INCLUDE));
        assertNotNull(granules);
        assertFalse(granules.isEmpty());
        for (CoverageSlice slice : granules) {
          final SimpleFeature sf = slice.getOriginator();
          if (TestData.isInteractiveTest()) {
            LOGGER.info(DataUtilities.encodeFeature(sf));
          }

          // checks
          for (Property p : sf.getProperties()) {
            assertNotNull("Property " + p.getName() + " had a null value!", p.getValue());
          }
        }
      }
    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
    }
  }
 protected void cleanEmptyElements(Feature target) throws DataSourceException {
   try {
     ArrayList values = new ArrayList<Property>();
     for (Iterator i = target.getValue().iterator(); i.hasNext(); ) {
       Property p = (Property) i.next();
       if (hasChild(p) || p.getDescriptor().getMinOccurs() > 0 || getEncodeIfEmpty(p)) {
         values.add(p);
       }
     }
     target.setValue(values);
   } catch (DataSourceException e) {
     throw new DataSourceException("Unable to clean empty element", e);
   }
 }
    public BugInfo(SimpleFeature next, long idx) throws IndexOutOfBoundsException, ParseException {

      bugId = idx;
      attributes = new HashMap<>();
      Collection<Property> properties = next.getProperties();
      Iterator<Property> it = properties.iterator();
      while (it.hasNext()) {
        Property p = it.next();
        attributes.put(p.getName().toString(), p.getValue().toString());
      }
      this.geom = (Geometry) next.getAttribute(0);
      this.desc = (String) next.getAttribute("error_desc");
      this.id = next.getID();
      name = next.getName();
    }
 private boolean getEncodeIfEmpty(Property p) {
   Object o = ((p.getDescriptor()).getUserData().get("encodeIfEmpty"));
   if (o == null) {
     return false;
   }
   return (Boolean) o;
 }
  /**
   * Parses SimpleFeature typed object recursively to Map - field names (keys) are combined with
   * parent sub property names, if properties in property
   *
   * @param feature
   * @return feature all properties as a HashMap
   */
  private static void parseFeatureProperties(Map result, SimpleFeature feature) {

    for (Property prop : feature.getProperties()) {
      String field = prop.getName().toString();
      Object value = feature.getAttribute(field);
      if (value != null) { // hide null properties
        if (value instanceof Map) {
          parseFeaturePropertiesMap(result, (Map) value, field);
        } else if (value instanceof List) {
          parseFeaturePropertiesMapList(result, (List) value, field);
        } else {

          result.put(field, value.toString());
        }
      }
    }
  }
 @Override
 public String[] encode(SimpleFeature feature) {
   List<String> csvRecord = new ArrayList<String>();
   for (Property property : feature.getProperties()) {
     Object value = property.getValue();
     if (value == null) {
       csvRecord.add("");
     } else if (value instanceof Point) {
       Point point = (Point) value;
       csvRecord.add(Double.toString(point.getY()));
       csvRecord.add(Double.toString(point.getX()));
     } else {
       String txt = value.toString();
       csvRecord.add(txt);
     }
   }
   return csvRecord.toArray(new String[csvRecord.size() - 1]);
 }
  /**
   * Set xlink:href client property for multi-valued chained features. This has to be specially
   * handled because we don't want to encode the nested features attributes, since it's already an
   * xLink. Also we need to eliminate duplicates.
   *
   * @param target The target attribute
   * @param clientPropsMappings Client properties mappings
   * @param value Nested features
   * @param xpath Attribute xPath where the client properties are to be set
   * @param targetNodeType Target node type
   */
  protected void setXlinkReference(
      Attribute target,
      Map<Name, Expression> clientPropsMappings,
      Object value,
      StepList xpath,
      AttributeType targetNodeType) {
    Expression linkExpression = clientPropsMappings.get(XLINK_HREF_NAME);

    for (Object singleVal : (Collection) value) {
      // Make sure the same value isn't already set
      // in case it comes from a denormalized view for many-to-many relationship.
      // (1) Get the first existing value
      Collection<Property> existingAttributes = getProperties((ComplexAttribute) target, xpath);
      boolean exists = false;

      if (existingAttributes != null) {
        for (Property existingAttribute : existingAttributes) {
          Object existingValue = existingAttribute.getUserData().get(Attributes.class);
          if (existingValue != null) {
            assert existingValue instanceof HashMap;
            existingValue = ((Map) existingValue).get(XLINK_HREF_NAME);
          }
          if (existingValue != null) {
            Object hrefValue = linkExpression.evaluate(singleVal);
            if (hrefValue != null && hrefValue.equals(existingValue)) {
              // (2) if one of the new values matches the first existing value,
              // that means this comes from a denormalized view,
              // and this set has already been set
              exists = true;
              // stop looking once found
              break;
            }
          }
        }
      }
      if (!exists) {
        Attribute instance =
            xpathAttributeBuilder.set(target, xpath, null, null, targetNodeType, true, null);
        setClientProperties(instance, singleVal, clientPropsMappings);
      }
    }
  }
Esempio n. 16
0
  /**
   * Constructs a new {@link RevFeature} from the provided {@link Feature}.
   *
   * @param feature the feature to build from
   * @return the newly constructed RevFeature
   */
  public RevFeature build(Feature feature) {
    if (feature == null) {
      throw new IllegalStateException("No feature set");
    }

    Collection<Property> props = feature.getProperties();

    List<Property> list = new ArrayList<Property>(props);

    Collections.sort(list, PROPERTY_ORDER);

    ImmutableList.Builder<Optional<Object>> valuesBuilder =
        new ImmutableList.Builder<Optional<Object>>();

    for (Property prop : list) {
      valuesBuilder.add(Optional.fromNullable(prop.getValue()));
    }

    return RevFeature.build(valuesBuilder.build());
  }
  protected Map getClientProperties(Property attribute) throws DataSourceException {

    Map<Object, Object> userData = attribute.getUserData();
    Map clientProperties = new HashMap<Name, Expression>();
    if (userData != null && userData.containsKey(Attributes.class)) {
      Map props = (Map) userData.get(Attributes.class);
      if (!props.isEmpty()) {
        clientProperties.putAll(props);
      }
    }
    return clientProperties;
  }
Esempio n. 18
0
 @Override
 public List<Object> getAttributes() {
   //
   // Get properties
   //
   List<Property> eList = getProperties();
   //
   // Initialize value list
   //
   List<Object> values = EFeatureUtils.newList(eList.size());
   //
   // Loop over all property instances
   //
   for (Property it : eList) {
     values.add(it.getValue());
   }
   //
   // Finished
   //
   return values;
 }
Esempio n. 19
0
  public static void featureToJson(
      org.opengis.feature.Feature feature, JSONBuilder json, boolean returnGeometry) {
    GeometryAttribute geometry = feature.getDefaultGeometryProperty();
    json.object();
    if (returnGeometry) {
      json.key("geometry");
      GeometryEncoder.toJson((com.vividsolutions.jts.geom.Geometry) geometry.getValue(), json);
    }
    json.key("attributes");
    json.object();

    json.key("objectid").value(adaptId(feature.getIdentifier().getID()));

    for (Property prop : feature.getProperties()) {
      if (geometry == null || !prop.getName().equals(geometry.getName())) {
        final Object value;
        if (prop.getValue() instanceof java.util.Date) {
          value = ((java.util.Date) prop.getValue()).getTime();
        } else {
          value = prop.getValue();
        }
        json.key(prop.getName().getLocalPart()).value(value);
      }
    }

    json.endObject();
    json.endObject();
  }
Esempio n. 20
0
  public ShapeWrappers extractFeatureProperties(Feature f) {
    HashMap<String, String> propMap = new HashMap<String, String>();
    String shapeText = f.getDefaultGeometryProperty().getValue().toString();

    Collection<Property> props = f.getProperties();
    Iterator<Property> pi = props.iterator();

    while (pi.hasNext()) {
      Property p = pi.next();
      String name = p.getName().toString();
      // System.out.println(name);
      if (pattern != null) {
        if (propNames.contains(name) || pattern.matcher(name).find()) {
          propMap.put(p.getName().toString(), p.getValue().toString());
        }
      } else {
        if (propNames.contains(name)) {
          try {
            propMap.put(p.getName().toString(), p.getValue().toString());
          } catch (Exception e) {
            log.log(Level.WARNING, "{0} prop name  {1}", new Object[] {e.getMessage(), name});
          }
        }
      }
    }
    return new ShapeWrappers(shapeText, propMap);
  }
 /**
  * Encode any client properties (XML attributes) found in the UserData map of a ComplexAttribute
  * as XML attributes of the element.
  *
  * @param complex the ComplexAttribute to search for client properties
  * @param element the element to which XML attributes should be added
  */
 @SuppressWarnings("unchecked")
 public static void encodeClientProperties(Property complex, Element element) {
   Map<Name, Object> clientProperties =
       (Map<Name, Object>) complex.getUserData().get(Attributes.class);
   if (clientProperties != null) {
     for (Name name : clientProperties.keySet()) {
       if (clientProperties.get(name) != null) {
         element.setAttributeNS(
             name.getNamespaceURI(), name.getLocalPart(), clientProperties.get(name).toString());
       }
     }
   }
 }
  private boolean hasChild(Property p) throws DataSourceException {
    boolean result = false;
    if (p.getValue() instanceof Collection) {

      Collection c = (Collection) p.getValue();

      if (this.getClientProperties(p).containsKey(XLINK_HREF_NAME)) {
        return true;
      }

      ArrayList values = new ArrayList();
      for (Object o : c) {
        if (o instanceof Property) {
          if (hasChild((Property) o)) {
            values.add(o);
            result = true;
          } else if (getEncodeIfEmpty((Property) o)) {
            values.add(o);
            result = true;
          } else if (((Property) o).getDescriptor().getMinOccurs() > 0) {
            if (((Property) o).getDescriptor().isNillable()) {
              // add nil mandatory property
              values.add(o);
            }
          }
        }
      }
      p.setValue(values);
    } else if (p.getName().equals(ComplexFeatureConstants.FEATURE_CHAINING_LINK_NAME)) {
      // ignore fake attribute FEATURE_LINK
      result = false;
    } else if (p.getValue() != null && p.getValue().toString().length() > 0) {
      result = true;
    }
    return result;
  }
  @Override
  /**
   * @param inputData a HashMap of the input data: inputObservations: the observations
   *     inputAuthoritativeData: the authoritative points inputDistance: the distance threshold
   *     minNumber: the minimum number of named features to pass fieldName: the name of the field
   *     within the observations to match
   * @result results a HashpMap of the results: result: the input data with the polygon attributes
   *     attached, null values for no match qual_result: the matched input only data with polygon
   *     attributes attached
   */
  public Map<String, IData> run(Map<String, List<IData>> inputData) throws ExceptionReport {

    List obsList = inputData.get("inputObservations");
    List authList = inputData.get("inputAuthoritativeData");
    List distList = inputData.get("inputDistance");
    List minList = inputData.get("minNumber");
    List fieldList = inputData.get("fieldName");

    FeatureCollection obsFc = ((GTVectorDataBinding) obsList.get(0)).getPayload();
    FeatureCollection authFc = ((GTVectorDataBinding) authList.get(0)).getPayload();
    double dist = ((LiteralDoubleBinding) distList.get(0)).getPayload();
    int minNum = ((LiteralIntBinding) minList.get(0)).getPayload();
    String fieldName = ((LiteralStringBinding) fieldList.get(0)).getPayload();

    ArrayList<SimpleFeature> resultFeatures = new ArrayList<SimpleFeature>();
    ArrayList<SimpleFeature> returnFeatures = new ArrayList<SimpleFeature>();

    SimpleFeatureIterator obsIt = (SimpleFeatureIterator) obsFc.features();
    SimpleFeatureIterator authIt = (SimpleFeatureIterator) authFc.features();

    SimpleFeatureIterator sfi = (SimpleFeatureIterator) obsFc.features();
    SimpleFeatureType fType = null;
    SimpleFeature tempPropFeature = sfi.next();
    CoordinateReferenceSystem inputObsCrs = obsFc.getSchema().getCoordinateReferenceSystem();

    Collection<Property> obsProp = tempPropFeature.getProperties();

    SimpleFeatureTypeBuilder resultTypeBuilder = new SimpleFeatureTypeBuilder();
    resultTypeBuilder.setName("typeBuilder");
    resultTypeBuilder.setCRS(inputObsCrs);

    Iterator<Property> pItObs = obsProp.iterator();

    sfi.close();
    while (pItObs.hasNext() == true) {

      try {

        Property tempProp = pItObs.next();
        PropertyType type = tempProp.getDescriptor().getType();
        String name = type.getName().getLocalPart();
        Class<String> valueClass = (Class<String>) tempProp.getType().getBinding();

        resultTypeBuilder.add(name, valueClass);

        // LOG.warn ("Obs property " + name + " " + valueClass + " " +type.toString());
      } catch (Exception e) {
        LOG.error("property error " + e);
      }
    }

    // add DQ_Field

    resultTypeBuilder.add("DQ_SA_SimilarF", Double.class);
    SimpleFeatureType typeF = resultTypeBuilder.buildFeatureType();
    // LOG.warn("Get Spatial Accuracy Feature Type " + typeF.toString());

    SimpleFeatureBuilder resultFeatureBuilder = new SimpleFeatureBuilder(typeF);

    obsIt.close();

    SimpleFeatureIterator obsIt2 = (SimpleFeatureIterator) obsFc.features();

    while (obsIt2.hasNext()) {
      SimpleFeature tempFeature = obsIt2.next();

      fType = tempFeature.getType();

      // LOG.warn("fieldName " + fieldName + " featureName " + featureN + " tempFeature Type " +
      // fType);
      // LOG.warn("TableFeatureName " + tempFeature.getProperty(fieldName).getValue());

      String tempFeatureName = (String) tempFeature.getProperty(fieldName).getValue();

      Geometry geom = (Geometry) tempFeature.getDefaultGeometry();
      for (Property obsProperty : tempFeature.getProperties()) {

        String name = obsProperty.getName().toString();
        Object value = obsProperty.getValue();

        resultFeatureBuilder.set(name, value);
      }

      Geometry bufferGeom = geom.buffer(dist);

      SimpleFeatureIterator authIt2 = (SimpleFeatureIterator) authFc.features();

      int count = 0;
      int within = 0;

      while (authIt2.hasNext() && count <= minNum) {
        SimpleFeature tempAuth = authIt2.next();
        String featureN = (String) tempAuth.getProperty(fieldName).getValue();

        Geometry tempGeom = (Geometry) tempAuth.getDefaultGeometry();

        String authProperty = (String) tempAuth.getProperty(fieldName).getValue();

        if (tempGeom.within(bufferGeom) && authProperty.equalsIgnoreCase(featureN)) {

          count++;

          if (count >= minNum) {
            within = 1;
          }
        }
      }
      LOG.warn("HERE " + within);
      resultFeatureBuilder.set("DQ_SA_SimilarF", within);

      SimpleFeature result = resultFeatureBuilder.buildFeature(tempFeature.getID());
      result.setDefaultGeometry(geom);
      LOG.warn("HERE " + result);
      returnFeatures.add(result);

      if (within == 1) {
        resultFeatures.add(result);
      }

      authIt2.close();
    }
    obsIt2.close();

    ListFeatureCollection qualResult = new ListFeatureCollection(fType, resultFeatures);
    ListFeatureCollection returns = new ListFeatureCollection(fType, returnFeatures);

    LOG.warn("HERE 2 " + qualResult.size() + " " + returns.size());

    Map<String, IData> results = new HashMap<String, IData>();
    results.put("qual_result", new GTVectorDataBinding(qualResult));
    results.put("result", new GTVectorDataBinding(returns));

    return results;
  }
  @Override
  /**
   * inputData a HashMap of the input data:
   *
   * @param inputObservations: the observations
   * @param inputAuthoritativeData: the polygons
   * @param bufferSize: the size of the buffer around the polygons results a HashpMap of the
   *     results:
   * @result result: the input data with the polygon attributes attached, null values for no match
   * @result qual_result: the matched input only data with polygon attributes attached
   */
  public Map<String, IData> run(Map<String, List<IData>> inputData) throws ExceptionReport {

    HashMap<String, Object> metadataMap = new HashMap<String, Object>();
    ArrayList<SimpleFeature> list = new ArrayList<SimpleFeature>();
    List<IData> inputObs = inputData.get("inputObservations");
    List<IData> inputAuth = inputData.get("inputAuthoritativeData");
    List<IData> inputLit = inputData.get("bufferSize");
    IData observations = inputObs.get(0);
    IData authoritative = inputAuth.get(0);

    IData buffersize = inputLit.get(0);

    double doubleB = (Double) buffersize.getPayload();

    FeatureCollection obsFC = ((GTVectorDataBinding) observations).getPayload();
    FeatureCollection authFC = ((GTVectorDataBinding) authoritative).getPayload();

    SimpleFeatureIterator obsIt = (SimpleFeatureIterator) obsFC.features();

    SimpleFeatureIterator authIt = (SimpleFeatureIterator) authFC.features();

    // setup result feature

    SimpleFeature obsItFeat = obsIt.next();

    SimpleFeature obsItAuth = authIt.next();

    Collection<Property> property = obsItFeat.getProperties();
    Collection<Property> authProperty = obsItAuth.getProperties();

    // setup result type builder
    SimpleFeatureTypeBuilder resultTypeBuilder = new SimpleFeatureTypeBuilder();
    resultTypeBuilder.setName("typeBuilder");

    Iterator<Property> pItObs = property.iterator();
    Iterator<Property> pItAuth = authProperty.iterator();

    metadataMap.put("element", "elementBufferedMetadata");
    File metadataFile = createXMLMetadata(metadataMap);

    while (pItObs.hasNext() == true) {

      try {
        Property tempProp = pItObs.next();

        PropertyType type = tempProp.getDescriptor().getType();
        String name = type.getName().getLocalPart();
        Class<String> valueClass = (Class<String>) tempProp.getType().getBinding();

        resultTypeBuilder.add(name, valueClass);

      } catch (Exception e) {
        LOGGER.error("property error " + e);
      }
    }
    int i = 0;
    while (pItAuth.hasNext() == true) {
      try {
        Property tempProp = pItAuth.next();

        PropertyType type = tempProp.getDescriptor().getType();
        String name = type.getName().getLocalPart();
        Class<String> valueClass = (Class<String>) tempProp.getType().getBinding();

        if (i > 3) {

          resultTypeBuilder.add(name, valueClass);
        }

        i++;

      } catch (Exception e) {
        LOGGER.error("property error " + e);
      }
    }

    obsIt.close();
    authIt.close();
    resultTypeBuilder.add("withinBuffer", Integer.class);

    // set up result feature builder

    SimpleFeatureType type = resultTypeBuilder.buildFeatureType();
    SimpleFeatureBuilder resultFeatureBuilder = new SimpleFeatureBuilder(type);

    // process data here:

    SimpleFeatureIterator obsIt2 = (SimpleFeatureIterator) obsFC.features();

    int within = 0;

    FeatureCollection resultFeatureCollection = DefaultFeatureCollections.newCollection();

    while (obsIt2.hasNext() == true) {
      within = 0;
      SimpleFeature tempObs = obsIt2.next();
      Geometry obsGeom = (Geometry) tempObs.getDefaultGeometry();

      for (Property obsProperty : tempObs.getProperties()) {

        String name = obsProperty.getName().getLocalPart();
        Object value = obsProperty.getValue();

        resultFeatureBuilder.set(name, value);
        // LOGGER.warn("obs Property set " + name);
      }

      double bufferSizeDouble = doubleB;

      Geometry bufferGeom = obsGeom.buffer(bufferSizeDouble);

      int j = 0;
      SimpleFeatureIterator authIt2 = (SimpleFeatureIterator) authFC.features();
      while (authIt2.hasNext() == true) {

        SimpleFeature tempAuth = authIt2.next();
        Geometry authGeom = (Geometry) tempAuth.getDefaultGeometry();

        if (bufferGeom.intersects(authGeom) == true) {
          within = 1;
          j = 0;

          LOGGER.warn("Intersection = true");
          for (Property authProperty1 : tempAuth.getProperties()) {

            String name = authProperty1.getName().getLocalPart();
            Object value = authProperty1.getValue();
            // Class valueClass = (Class<String>)authProperty1.getType().getBinding();

            //		LOGGER.warn("Auth property " + name);
            if (j > 3) {
              resultFeatureBuilder.set(name, value);
              //	LOGGER.warn("Auth property set " + name);

            }

            j++;
          }
        }
      }
      resultFeatureBuilder.set("withinBuffer", within);

      SimpleFeature resultFeature = resultFeatureBuilder.buildFeature(tempObs.getName().toString());
      Geometry geom = (Geometry) tempObs.getDefaultGeometry();
      resultFeature.setDefaultGeometry(geom);

      list.add(resultFeature);

      // resultFeatureCollection.add(resultFeature);
      // LOGGER.warn("RESULT FEATURE " + resultFeatureCollection.getSchema().toString());
      // resultFeatureCollection = obsFC;
    }

    ListFeatureCollection listFeatureCollection = new ListFeatureCollection(type, list);
    LOGGER.warn("Result Feature Size " + listFeatureCollection.size());

    // sort HashMap
    GenericFileData gf = null;
    try {
      gf = new GenericFileData(metadataFile, "text/xml");
    } catch (IOException e) {
      LOGGER.error("GenericFileData " + e);
    }

    HashMap<String, IData> results = new HashMap<String, IData>();
    results.put("result", new GTVectorDataBinding((FeatureCollection) obsFC));
    results.put("qual_result", new GTVectorDataBinding((FeatureCollection) listFeatureCollection));
    results.put("metadata", new GenericFileDataBinding(gf));

    return results;
  }
Esempio n. 25
0
  @Test
  public void testImageReaderPolyphemunsComplex() throws Exception {
    File file = null;
    try {
      file = TestData.file(this, "polyphemus_20130301.nc");
    } catch (IOException e) {
      LOGGER.warning("Unable to find file polyphemus_20130301.nc");
      return;
    }
    if (!file.exists()) {
      LOGGER.warning("Unable to find file polyphemus_20130301.nc");
      return;
    }
    final NetCDFImageReaderSpi unidataImageReaderSpi = new NetCDFImageReaderSpi();
    assertTrue(unidataImageReaderSpi.canDecodeInput(file));
    NetCDFImageReader reader = null;
    try {
      reader = (NetCDFImageReader) unidataImageReaderSpi.createReaderInstance();
      reader.setInput(file);
      int numImages = reader.getNumImages(true);
      assertEquals(1008, numImages);
      for (int i = 0; i < numImages; i++) {
        Slice2DIndex sliceIndex = reader.getSlice2DIndex(i);
        assertNotNull(sliceIndex);
        spitOutSliceInformation(i, sliceIndex);
      }

      // check coverage names
      final List<Name> names = reader.getCoveragesNames();
      assertNotNull(names);
      assertTrue(!names.isEmpty());
      assertTrue(3 == names.size());
      assertTrue(names.contains(new NameImpl("NO2")));
      assertTrue(names.contains(new NameImpl("O3")));
      assertTrue(names.contains(new NameImpl("V")));

      // checking slice catalog
      final CoverageSlicesCatalog cs = reader.getCatalog();
      assertNotNull(cs);

      // get typenames
      final String[] typeNames = cs.getTypeNames();
      for (String typeName : typeNames) {
        final List<CoverageSlice> granules = cs.getGranules(new Query(typeName, Filter.INCLUDE));
        assertNotNull(granules);
        assertFalse(granules.isEmpty());
        for (CoverageSlice slice : granules) {
          final SimpleFeature sf = slice.getOriginator();
          if (TestData.isInteractiveTest()) {
            LOGGER.info(DataUtilities.encodeFeature(sf));
          }

          // checks
          for (Property p : sf.getProperties()) {
            assertNotNull("Property " + p.getName() + " had a null value!", p.getValue());
          }
        }
      }
    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
    }
  }
Esempio n. 26
0
  @Test
  public void testImageReaderPolyphemunsComplex2() throws Exception {
    File file = null;
    try {
      file = TestData.file(this, "polyphemus_20130301.nc");
    } catch (IOException e) {
      LOGGER.warning("Unable to find file polyphemus_20130301.nc");
      return;
    }
    if (!file.exists()) {
      LOGGER.warning("Unable to find file polyphemus_20130301.nc");
      return;
    }
    FileUtils.copyFile(file, new File(TestData.file(this, null), "polyphemus.nc"));
    file = TestData.file(this, "polyphemus.nc");
    final NetCDFImageReaderSpi unidataImageReaderSpi = new NetCDFImageReaderSpi();
    assertTrue(unidataImageReaderSpi.canDecodeInput(file));
    NetCDFImageReader reader = null;
    try {
      reader = (NetCDFImageReader) unidataImageReaderSpi.createReaderInstance();
      reader.setInput(file);
      int numImages = reader.getNumImages(true);
      assertEquals(1008, numImages);
      for (int i = 0; i < numImages; i++) {
        Slice2DIndex sliceIndex = reader.getSlice2DIndex(i);
        assertNotNull(sliceIndex);
        spitOutSliceInformation(i, sliceIndex);
      }

      // check dimensions
      CoverageSourceDescriptor cd = reader.getCoverageDescriptor(new NameImpl("NO2"));
      final List<AdditionalDomain> additionalDomains = cd.getAdditionalDomains();
      assertNull(additionalDomains);

      final List<DimensionDescriptor> dimensions = cd.getDimensionDescriptors();
      assertNotNull(dimensions);
      assertTrue(!dimensions.isEmpty());
      assertEquals("wrong dimensions", 2, dimensions.size());
      DimensionDescriptor dim = dimensions.get(0);
      assertTrue(dim.getName().equals("TIME"));
      assertTrue(dim.getStartAttribute().equals("time"));
      dim = dimensions.get(1);
      assertTrue(dim.getName().equals("ELEVATION"));
      assertTrue(dim.getStartAttribute().equals("z"));

      // check coverage names
      final List<Name> names = reader.getCoveragesNames();
      assertNotNull(names);
      assertTrue(!names.isEmpty());
      assertTrue(3 == names.size());
      assertTrue(names.contains(new NameImpl("NO2")));
      assertTrue(names.contains(new NameImpl("O3")));
      assertTrue(names.contains(new NameImpl("V")));

      // checking slice catalog
      final CoverageSlicesCatalog cs = reader.getCatalog();
      assertNotNull(cs);

      // get typenames
      final String[] typeNames = cs.getTypeNames();
      for (String typeName : typeNames) {
        final List<CoverageSlice> granules = cs.getGranules(new Query(typeName, Filter.INCLUDE));
        assertNotNull(granules);
        assertFalse(granules.isEmpty());
        for (CoverageSlice slice : granules) {
          final SimpleFeature sf = slice.getOriginator();
          if (TestData.isInteractiveTest()) {
            LOGGER.info(DataUtilities.encodeFeature(sf));
          }

          // checks
          for (Property p : sf.getProperties()) {
            assertNotNull("Property " + p.getName() + " had a null value!", p.getValue());
          }
        }
      }
    } finally {

      // close reader
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }

      // specific clean up
      FileUtils.deleteDirectory(TestData.file(this, ".polyphemus"));
    }
  }
Esempio n. 27
0
  @Test
  public void testImageReaderAscat() throws Exception {
    File file = null;
    try {
      file = TestData.file(this, "ascatl1.nc");
    } catch (IOException e) {
      LOGGER.warning("Unable to find file ascatl1.nc");
      return;
    }
    if (!file.exists()) {
      LOGGER.warning("Unable to find file ascatl1.nc");
      return;
    }
    final NetCDFImageReaderSpi unidataImageReaderSpi = new NetCDFImageReaderSpi();
    assertTrue(unidataImageReaderSpi.canDecodeInput(file));
    NetCDFImageReader reader = null;
    try {
      reader = (NetCDFImageReader) unidataImageReaderSpi.createReaderInstance();
      reader.setInput(file);
      int numImages = reader.getNumImages(true);
      for (int i = 0; i < numImages; i++) {
        Slice2DIndex sliceIndex = reader.getSlice2DIndex(i);
        assertNotNull(sliceIndex);
        spitOutSliceInformation(i, sliceIndex);
      }

      // check coverage names
      final List<Name> names = reader.getCoveragesNames();
      assertNotNull(names);
      assertTrue(!names.isEmpty());
      assertTrue(2 == names.size());
      assertEquals("cell_index", names.get(0).toString());
      assertEquals("f_land", names.get(1).toString());

      // checking slice catalog
      final CoverageSlicesCatalog cs = reader.getCatalog();
      assertNotNull(cs);

      // get typenames
      final String[] typeNames = cs.getTypeNames();
      for (String typeName : typeNames) {
        final List<CoverageSlice> granules = cs.getGranules(new Query(typeName, Filter.INCLUDE));
        assertNotNull(granules);
        assertFalse(granules.isEmpty());
        for (CoverageSlice slice : granules) {
          final SimpleFeature sf = slice.getOriginator();
          if (TestData.isInteractiveTest()) {
            LOGGER.info(DataUtilities.encodeFeature(sf));
          }

          // checks
          for (Property p : sf.getProperties()) {
            final String pName = p.getName().toString();
            if (!pName.equalsIgnoreCase("time") && !pName.equalsIgnoreCase("elevation")) {
              assertNotNull("Property " + p.getName() + " had a null value!", p.getValue());
            } else {
              assertNull("Property " + p.getName() + " did not have a null value!", p.getValue());
            }
          }
        }
      }

    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
    }
  }
  /**
   * Write the feature into the stream.
   *
   * @param feature The feature
   * @param root
   * @throws XMLStreamException
   */
  public Element writeFeature(final Feature feature, final Document rootDocument, boolean fragment)
      throws ParserConfigurationException {

    final Document document;
    if (rootDocument == null) {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      // then we have to create document-loader:
      factory.setNamespaceAware(false);
      DocumentBuilder loader = factory.newDocumentBuilder();

      // creating a new DOM-document...
      document = loader.newDocument();
    } else {
      document = rootDocument;
    }

    // the root element of the xml document (type of the feature)
    final FeatureType type = feature.getType();
    final Name typeName = type.getName();
    final String namespace = typeName.getNamespaceURI();
    final String localPart = typeName.getLocalPart();

    final Element rootElement;
    final Prefix prefix;
    if (namespace != null) {
      prefix = getPrefix(namespace);
      rootElement = document.createElementNS(namespace, localPart);
      rootElement.setPrefix(prefix.prefix);

    } else {
      rootElement = document.createElement(localPart);
      prefix = null;
    }
    // if main document set the xmlns
    if (!fragment) {
      rootElement.setAttributeNS(
          "http://www.w3.org/2000/xmlns/", "xmlns:gml", "http://www.opengis.net/gml");
    }
    final Attr idAttr = document.createAttributeNS(Namespaces.GML, "id");
    idAttr.setValue(feature.getIdentifier().getID());
    idAttr.setPrefix("gml");
    rootElement.setAttributeNodeNS(idAttr);

    if (rootDocument == null) {
      document.appendChild(rootElement);
    }
    // write properties in the type order
    for (final PropertyDescriptor desc : type.getDescriptors()) {
      final Collection<Property> props = feature.getProperties(desc.getName());
      for (Property a : props) {
        final Object valueA = a.getValue();
        final PropertyType typeA = a.getType();
        final Name nameA = a.getName();
        final String nameProperty = nameA.getLocalPart();
        String namespaceProperty = nameA.getNamespaceURI();
        if (valueA instanceof Collection && !(typeA instanceof GeometryType)) {
          for (Object value : (Collection) valueA) {
            final Element element;
            if (namespaceProperty != null) {
              element = document.createElementNS(namespaceProperty, nameProperty);
            } else {
              element = document.createElement(nameProperty);
            }
            element.setTextContent(Utils.getStringValue(value));
            if (prefix != null) {
              element.setPrefix(prefix.prefix);
            }
            rootElement.appendChild(element);
          }

        } else if (valueA instanceof Map && !(typeA instanceof GeometryType)) {
          final Map<?, ?> map = (Map) valueA;
          for (Entry<?, ?> entry : map.entrySet()) {
            final Element element;
            if (namespaceProperty != null) {
              element = document.createElementNS(namespaceProperty, nameProperty);
            } else {
              element = document.createElement(nameProperty);
            }
            final Object key = entry.getKey();
            if (key != null) {
              element.setAttribute("name", (String) key);
            }
            element.setTextContent(Utils.getStringValue(entry.getValue()));
            if (prefix != null) {
              element.setPrefix(prefix.prefix);
            }
            rootElement.appendChild(element);
          }

        } else if (!(typeA instanceof GeometryType)) {
          String value = Utils.getStringValue(valueA);
          if (value != null || (value == null && !a.isNillable())) {

            if ((nameProperty.equals("name") || nameProperty.equals("description"))
                && !Namespaces.GML.equals(namespaceProperty)) {
              namespaceProperty = Namespaces.GML;
              LOGGER.warning(
                  "the property name and description of a feature must have the GML namespace");
            }
            final Element element;
            if (namespaceProperty != null) {
              element = document.createElementNS(namespaceProperty, nameProperty);
            } else {
              element = document.createElement(nameProperty);
            }
            if (value != null) {
              element.setTextContent(value);
            }
            if (prefix != null) {
              element.setPrefix(prefix.prefix);
            }
            rootElement.appendChild(element);
          }

          // we add the geometry
        } else {

          if (valueA != null) {
            final Element element;
            if (namespaceProperty != null) {
              element = document.createElementNS(namespaceProperty, nameProperty);
            } else {
              element = document.createElement(nameProperty);
            }
            if (prefix != null) {
              element.setPrefix(prefix.prefix);
            }
            Geometry isoGeometry =
                JTSUtils.toISO(
                    (com.vividsolutions.jts.geom.Geometry) valueA,
                    type.getCoordinateReferenceSystem());
            Marshaller marshaller = null;
            try {
              marshaller = POOL.acquireMarshaller();
              marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
              marshaller.marshal(OBJECT_FACTORY.buildAnyGeometry(isoGeometry), element);
            } catch (JAXBException ex) {
              LOGGER.log(
                  Level.WARNING,
                  "JAXB Exception while marshalling the iso geometry: " + ex.getMessage(),
                  ex);
            } finally {
              if (marshaller != null) {
                POOL.release(marshaller);
              }
            }
            rootElement.appendChild(element);
          }
        }
      }
    }

    // writer.writeEndElement();
    return rootElement;
  }
Esempio n. 29
0
 @Override
 public int compare(Property left, Property right) {
   return RevFeatureType.PROPERTY_ORDER.compare(left.getDescriptor(), right.getDescriptor());
 }
  /**
   * Write SQL string to create tables in the test database based on the property files.
   *
   * @param propertyFiles Property files from app-schema-test suite.
   * @param parser The parser (WKT or an SC4O one for 3D tests)
   * @throws IllegalAttributeException
   * @throws NoSuchElementException
   * @throws IOException
   */
  private void createTables(Map<String, File> propertyFiles, String parser)
      throws IllegalAttributeException, NoSuchElementException, IOException {

    StringBuffer buf = new StringBuffer();
    StringBuffer spatialIndex = new StringBuffer();
    // drop table procedure I copied from Victor's Oracle_Data_ref_set.sql
    buf.append("CREATE OR REPLACE PROCEDURE DROP_TABLE_OR_VIEW(TabName in Varchar2) IS ")
        .append("temp number:=0;")
        .append(" tes VARCHAR2 (200) := TabName;")
        .append(" drp_stmt VARCHAR2 (200):=null;")
        .append("BEGIN select count(*) into temp from user_tables where TABLE_NAME = tes;")
        .append("if temp = 1 then drp_stmt := 'Drop Table '||tes;")
        .append("EXECUTE IMMEDIATE drp_stmt;")
        // drop views too
        .append("else select count(*) into temp from user_views where VIEW_NAME = tes;")
        .append("if temp = 1 then drp_stmt := 'Drop VIEW '||tes;")
        .append("EXECUTE IMMEDIATE drp_stmt;end if;end if;")
        .append("EXCEPTION WHEN OTHERS THEN ")
        .append(
            "raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);")
        .append("END DROP_TABLE_OR_VIEW;\n");

    for (String fileName : propertyFiles.keySet()) {
      File file = new File(propertyFiles.get(fileName), fileName);

      try (PropertyFeatureReader reader = new PropertyFeatureReader("test", file)) {
        SimpleFeatureType schema = reader.getFeatureType();
        String tableName = schema.getName().getLocalPart().toUpperCase();
        // drop table if exists
        buf.append("CALL DROP_TABLE_OR_VIEW('").append(tableName).append("')\n");
        // create the table
        buf.append("CREATE TABLE ").append(tableName).append("(");
        // + pkey
        int size = schema.getAttributeCount() + 1;
        String[] fieldNames = new String[size];
        List<String> createParams = new ArrayList<String>();
        int j = 0;
        String type;
        String field;
        int spatialIndexCounter = 0;
        for (PropertyDescriptor desc : schema.getDescriptors()) {
          field = desc.getName().toString().toUpperCase();
          fieldNames[j] = field;
          if (desc instanceof GeometryDescriptor) {
            type = "SDO_GEOMETRY";
            // Update spatial index
            int srid = getSrid(((GeometryType) desc.getType()));

            spatialIndex
                .append("DELETE FROM user_sdo_geom_metadata WHERE table_name = '")
                .append(tableName)
                .append("'\n");

            spatialIndex
                .append("Insert into user_sdo_geom_metadata ")
                .append("(TABLE_NAME,COLUMN_NAME,DIMINFO,SRID)")
                .append("values ('")
                .append(tableName)
                .append("','")
                .append(field)
                .append("',MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X',140.962,144.909,0.00001),")
                .append("MDSYS.SDO_DIM_ELEMENT('Y',-38.858,-33.98,0.00001)")
                .append( // support 3d index
                    ((GeometryDescriptor) desc).getCoordinateReferenceSystem() != null
                            && ((GeometryDescriptor) desc)
                                    .getCoordinateReferenceSystem()
                                    .getCoordinateSystem()
                                    .getDimension()
                                == 3
                        ? ", MDSYS.SDO_DIM_ELEMENT('Z',-100000, 100000, 1) ),"
                        : "),")
                .append(srid)
                .append(")\n");

            // ensure it's <= 30 characters to avoid Oracle exception
            String indexName =
                (tableName.length() <= 26 ? tableName : tableName.substring(0, 26)) + "_IDX";
            if (spatialIndexCounter > 0) {
              // to avoid duplicate index name when there are > 1 geometry in the same table
              indexName += spatialIndexCounter;
            }

            spatialIndex
                .append("CREATE INDEX \"")
                .append(indexName)
                .append("\" ON \"")
                .append(tableName)
                .append("\"(\"")
                .append(field)
                .append("\") ")
                .append("INDEXTYPE IS \"MDSYS\".\"SPATIAL_INDEX\"\n");
            spatialIndexCounter++;
          } else {
            type = Classes.getShortName(desc.getType().getBinding());
            if (type.equalsIgnoreCase("String")) {
              type = "NVARCHAR2(250)";
            } else if (type.equalsIgnoreCase("Double")) {
              type = "NUMBER";
            }
            // etc. assign as required
          }
          createParams.add(field + " " + type);
          j++;
        }
        // Add numeric PK for sorting
        fieldNames[j] = "PKEY";
        createParams.add("PKEY VARCHAR2(30)");
        buf.append(StringUtils.join(createParams.iterator(), ", "));
        buf.append(")\n");
        buf.append(
            "ALTER TABLE " + tableName + " ADD CONSTRAINT " + tableName + " PRIMARY KEY (PKEY)\n");
        // then insert rows
        SimpleFeature feature;
        FeatureId id;
        while (reader.hasNext()) {
          buf.append("INSERT INTO ").append(tableName).append("(");
          feature = reader.next();
          buf.append(StringUtils.join(fieldNames, ", "));
          buf.append(") ");
          buf.append("VALUES (");
          Collection<Property> properties = feature.getProperties();
          String[] values = new String[size];
          int valueIndex = 0;
          for (Property prop : properties) {
            Object value = prop.getValue();
            if (value instanceof Geometry) {
              // use wkt writer to convert geometry to string, so third dimension can be supported
              // if present.
              Geometry geom = (Geometry) value;
              value = new WKTWriter(geom.getCoordinate().z == Double.NaN ? 2 : 3).write(geom);
            }
            if (value == null || value.toString().equalsIgnoreCase("null")) {
              values[valueIndex] = "null";
            } else if (prop.getType() instanceof GeometryType) {
              int srid = getSrid(((GeometryType) prop.getType()));
              StringBuffer geomValue = new StringBuffer(parser + "('");
              geomValue.append(value).append("'");
              if (srid > -1) {
                // attach srid
                geomValue.append(", ").append(srid);
              }
              geomValue.append(")");
              values[valueIndex] = geomValue.toString();
            } else if (prop.getType().getBinding().getSimpleName().equalsIgnoreCase("DATE")) {
              values[valueIndex] = "TO_DATE('" + value + "', 'yyyy-MM-dd')";
            } else {
              values[valueIndex] = "'" + value + "'";
            }
            valueIndex++;
          }
          id = feature.getIdentifier();
          // insert primary key
          values[valueIndex] = "'" + id.toString() + "'";
          buf.append(StringUtils.join(values, ","));
          buf.append(")\n");
        }
      }
      buf.append(spatialIndex.toString());
      spatialIndex.delete(0, spatialIndex.length());
      if (buf.length() > 0) {
        this.sql = buf.toString();
      }
    }
  }