/** Compares this reader/input pair with the specified object for equality. */
 @Override
 public boolean equals(final Object object) {
   if (object instanceof ReaderInputPair) {
     final ReaderInputPair that = (ReaderInputPair) object;
     return Utilities.equals(this.reader, that.reader)
         && Utilities.deepEquals(this.input, that.input);
   }
   return false;
 }
 @Override
 public boolean equals(final Object other) {
   if (other instanceof CodePair) {
     final CodePair that = (CodePair) other;
     return Utilities.equals(this.source, that.source)
         && Utilities.equals(this.target, that.target);
   }
   return false;
 }
  /**
   * This method takes the input {@link SimpleFeatureCollection} and transforms it into a shapefile
   * using the provided file.
   *
   * <p>Make sure the provided files ends with .shp.
   *
   * @param fc the {@link SimpleFeatureCollection} to be encoded as a shapefile.
   * @param destination the {@link File} where we want to write the shapefile.
   * @throws IOException in case an {@link IOException} is thrown by the underlying code.
   */
  protected static void featureCollectionToShapeFile(
      final SimpleFeatureCollection fc, final File destination) throws IOException {

    //
    // checks
    //
    org.geotools.util.Utilities.ensureNonNull("fc", fc);
    Utilities.ensureNonNull("destination", destination);
    // checks on the file
    if (destination.exists()) {

      if (destination.isDirectory())
        throw new IOException("The provided destination maps to a directory:" + destination);

      if (!destination.canWrite())
        throw new IOException(
            "The provided destination maps to an existing file that cannot be deleted:"
                + destination);

      if (!destination.delete())
        throw new IOException(
            "The provided destination maps to an existing file that cannot be deleted:"
                + destination);
    }

    // real work
    final DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("url", destination.toURI().toURL());
    params.put("create spatial index", Boolean.TRUE);

    ShapefileDataStore store = null;
    Transaction transaction = null;
    try {
      store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
      store.createSchema(fc.getSchema());

      final SimpleFeatureStore featureStore =
          (SimpleFeatureStore) store.getFeatureSource(fc.getSchema().getName());
      transaction = featureStore.getTransaction();

      featureStore.addFeatures(fc);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

      if (transaction != null) {

        transaction.commit();
        transaction.close();
      }

      if (store != null) {
        store.dispose();
      }
    }
  }
 /**
  * Compare this reference system with the specified object for equality. If {@code
  * compareMetadata} is {@code true}, then all available properties are compared including
  * {@linkplain #getValidArea valid area} and {@linkplain #getScope scope}.
  *
  * @param object The object to compare to {@code this}.
  * @param compareMetadata {@code true} for performing a strict comparaison, or {@code false} for
  *     comparing only properties relevant to transformations.
  * @return {@code true} if both objects are equal.
  */
 @Override
 public boolean equals(final AbstractIdentifiedObject object, final boolean compareMetadata) {
   if (super.equals(object, compareMetadata)) {
     if (!compareMetadata) {
       return true;
     }
     final AbstractReferenceSystem that = (AbstractReferenceSystem) object;
     return Utilities.equals(domainOfValidity, that.domainOfValidity)
         && Utilities.equals(scope, that.scope);
   }
   return false;
 }
  /**
   * Compares this ExternalGraphi with another.
   *
   * <p>Two external graphics are equal if they have the same uri and format.
   *
   * @param oth The other External graphic.
   * @return True if this and the other external graphic are equal.
   */
  public boolean equals(Object oth) {
    if (this == oth) {
      return true;
    }

    if (oth instanceof ExternalGraphicImpl) {
      ExternalGraphicImpl other = (ExternalGraphicImpl) oth;

      return Utilities.equals(uri, other.uri) && Utilities.equals(format, other.format);
    }

    return false;
  }
  public void notifyChanged(Notification notification) {
    int featureId = notification.getFeatureID(target.getClass());
    if (featureId != XSDPackage.XSD_ELEMENT_DECLARATION__SUBSTITUTION_GROUP) {
      return;
    }

    if (notification.getEventType() != Notification.ADD) {
      return;
    }
    if (!(notification.getNewValue() instanceof XSDElementDeclaration)) {
      return;
    }

    XSDElementDeclaration el = (XSDElementDeclaration) notification.getNewValue();
    XSDElementDeclaration e = target;

    while (e != null) {
      synchronized (e) {
        ArrayList<Integer> toremove = new ArrayList();
        for (int i = 0; i < e.getSubstitutionGroup().size(); i++) {
          XSDElementDeclaration se = (XSDElementDeclaration) e.getSubstitutionGroup().get(i);
          if (se == null
              || (Utilities.equals(el.getTargetNamespace(), se.getTargetNamespace())
                  && Utilities.equals(el.getName(), se.getName()))) {
            toremove.add(i);
          }
        }

        // iterate back in reverse order and skip the last element as to keep the latest
        // version of the element
        ArrayList<XSDElementDeclaration> removed = new ArrayList();
        for (int i = toremove.size() - 2; i > -1; i--) {
          XSDElementDeclaration se =
              (XSDElementDeclaration) e.getSubstitutionGroup().remove(toremove.get(i).intValue());
          removed.add(e);
        }

        // set the removed elements sub affiliation to a clone of the actual element
        for (XSDElementDeclaration se : removed) {
          if (se != null && e.equals(se.getSubstitutionGroupAffiliation())) {
            XSDElementDeclaration clone =
                (XSDElementDeclaration) e.cloneConcreteComponent(false, false);
            clone.setTargetNamespace(GML.NAMESPACE);

            se.setSubstitutionGroupAffiliation(clone);
          }
        }
      }
      e = e.getSubstitutionGroupAffiliation();
    }
  }
Exemple #7
0
 /**
  * Logs a debug message for {@link #getServiceProvider} method. Note: we are not required to
  * insert the method name ({@code "GetServiceProvider"}) in the message because it is part of the
  * informations already stored by {@link LogRecord}, and formatted by the default {@link
  * java.util.logging.SimpleFormatter}.
  *
  * @param status {@code "ENTRY"}, {@code "RETURN"} or {@code "THROW"}, according {@link Logger}
  *     conventions.
  * @param category The category given to the {@link #getServiceProvider} method.
  * @param key The key being examined, or {@code null}.
  * @param message Optional message, or {@code null} if none.
  * @param type Optional class to format after the message, or {@code null}.
  */
 private static void debug(
     final String status,
     final Class<?> category,
     final Hints.Key key,
     final String message,
     final Class type) {
   final StringBuilder buffer = new StringBuilder(status);
   buffer
       .append(Utilities.spaces(Math.max(1, 7 - status.length())))
       .append('(')
       .append(Classes.getShortName(category));
   if (key != null) {
     buffer.append(", ").append(key);
   }
   buffer.append(')');
   if (message != null) {
     buffer.append(": ").append(message);
   }
   if (type != null) {
     buffer.append(' ').append(Classes.getShortName(type)).append('.');
   }
   final LogRecord record = new LogRecord(DEBUG_LEVEL, buffer.toString());
   record.setSourceClassName(FactoryRegistry.class.getName());
   record.setSourceMethodName("getServiceProvider");
   record.setLoggerName(LOGGER.getName());
   LOGGER.log(record);
 }
  /**
   * Creates a {@link SimpleFeatureType} that exposes a coverage as a collections of feature points,
   * mapping the centre of each pixel as a point plus all the bands as attributes.
   *
   * <p>The FID is the long that combines x+y*width.
   *
   * @param gc2d the {@link GridCoverage2D} to wrap.
   * @param geometryClass the class for the geometry.
   * @return a {@link SimpleFeatureType} or <code>null</code> in case we are unable to wrap the
   *     coverage
   */
  public static SimpleFeatureType createFeatureType(
      final GridCoverage2D gc2d, final Class<? extends Geometry> geometryClass) {

    // checks
    Utilities.ensureNonNull("gc2d", gc2d);

    // building a feature type for this coverage
    final SimpleFeatureTypeBuilder ftBuilder = new SimpleFeatureTypeBuilder();
    ftBuilder.setName(gc2d.getName().toString());
    ftBuilder.setNamespaceURI("http://www.geotools.org/");

    // CRS
    ftBuilder.setCRS(gc2d.getCoordinateReferenceSystem2D());
    //		ftBuilder.setCRS(DefaultEngineeringCRS.GENERIC_2D);

    // TYPE is as follows the_geom | band
    ftBuilder.setDefaultGeometry("the_geom");
    ftBuilder.add("the_geom", geometryClass);
    if (!geometryClass.equals(Point.class)) {
      ftBuilder.add("value", Double.class);
    } else {

      // get sample type on bands
      final GridSampleDimension[] sampleDimensions = gc2d.getSampleDimensions();
      for (GridSampleDimension sd : sampleDimensions) {
        final SampleDimensionType sdType = sd.getSampleDimensionType();
        final int dataBuffType = TypeMap.getDataBufferType(sdType);

        // TODO I think this should be a public utility inside the FeatureUtilities class
        @SuppressWarnings("rawtypes")
        final Class bandClass;
        switch (dataBuffType) {
          case DataBuffer.TYPE_BYTE:
            bandClass = Byte.class;
            break;
          case DataBuffer.TYPE_DOUBLE:
            bandClass = Double.class;
            break;
          case DataBuffer.TYPE_FLOAT:
            bandClass = Float.class;
            break;
          case DataBuffer.TYPE_INT:
            bandClass = Integer.class;
            break;
          case DataBuffer.TYPE_SHORT:
          case DataBuffer.TYPE_USHORT:
            bandClass = Short.class;
            break;
          case DataBuffer.TYPE_UNDEFINED:
          default:
            return null;
        }
        ftBuilder.add(sd.getDescription().toString(), bandClass);
      }
    }
    return ftBuilder.buildFeatureType();
  }
Exemple #9
0
 /**
  * Compares the specified object with this envelope for equality.
  *
  * @param object The object to compare with this envelope.
  * @return {@code true} if the given object is equals to this envelope.
  */
 @Override
 public boolean equals(final Object object) {
   if (super.equals(object)) {
     final CoordinateReferenceSystem otherCRS =
         (object instanceof Envelope2D) ? ((Envelope2D) object).crs : null;
     return Utilities.equals(crs, otherCRS);
   }
   return false;
 }
 /**
  * Returns {@code true} if the specified object is also a {@linkplain DirectPosition direct
  * position} with equals {@linkplain #getCoordinate coordinate} and {@linkplain
  * #getCoordinateReferenceSystem CRS}.
  *
  * @param object The object to compare with this position.
  * @return {@code true} if the given object is equals to this position.
  */
 @Override
 public boolean equals(final Object object) {
   if (object instanceof DirectPosition) {
     final DirectPosition that = (DirectPosition) object;
     final int dimension = getDimension();
     if (dimension == that.getDimension()) {
       for (int i = 0; i < dimension; i++) {
         if (!Utilities.equals(this.getOrdinate(i), that.getOrdinate(i))) {
           return false;
         }
       }
       if (Utilities.equals(
           this.getCoordinateReferenceSystem(), that.getCoordinateReferenceSystem())) {
         assert hashCode() == that.hashCode() : this;
         return true;
       }
     }
   }
   return false;
 }
Exemple #11
0
  /**
   * Function to calculate the area of a polygon, according to the algorithm defined at
   * http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
   *
   * @param polyPoints array of points in the polygon
   * @return area of the polygon defined by pgPoints
   */
  public static double area(Point2D[] polyPoints) {
    Utilities.ensureNonNull("polyPoints", polyPoints);
    int i, j, n = polyPoints.length;
    double area = 0;

    for (i = 0; i < n; i++) {
      j = (i + 1) % n;
      area += polyPoints[i].getX() * polyPoints[j].getY();
      area -= polyPoints[j].getX() * polyPoints[i].getY();
    }
    area /= 2.0;
    return (area);
  }
Exemple #12
0
 /**
  * Scans {@linkplain System#getProperties system properties} for any property keys defined in this
  * class, and add their values to the specified map of hints. For example if the {@value
  * #FORCE_LONGITUDE_FIRST_AXIS_ORDER} system property is defined, then the {@link
  * Hints#FORCE_LONGITUDE_FIRST_AXIS_ORDER FORCE_LONGITUDE_FIRST_AXIS_ORDER} hint will be added to
  * the set of hints.
  *
  * @return {@code true} if at least one hint changed as a result of this scan, or {@code false}
  *     otherwise.
  */
 static boolean scanForSystemHints(final Hints hints) {
   assert Thread.holdsLock(hints);
   boolean changed = false;
   synchronized (BINDINGS) {
     for (final Map.Entry<String, RenderingHints.Key> entry : BINDINGS.entrySet()) {
       final String propertyKey = entry.getKey();
       final String property;
       try {
         property = System.getProperty(propertyKey);
       } catch (SecurityException e) {
         unexpectedException(e);
         continue;
       }
       if (property != null) {
         /*
          * Converts the system property value from String to Object (java.lang.Boolean
          * or java.lang.Number). We perform this conversion only if the key is exactly
          * of kind Hints.Key,  not a subclass like ClassKey, in order to avoid useless
          * class loading on  'getValueClass()'  method invocation (ClassKey don't make
          * sense for Boolean and Number, which are the only types that we convert here).
          */
         Object value = property;
         final RenderingHints.Key hintKey = entry.getValue();
         if (hintKey.getClass().equals(Hints.Key.class)) {
           final Class<?> type = ((Hints.Key) hintKey).getValueClass();
           if (type.equals(Boolean.class)) {
             value = Boolean.valueOf(property);
           } else if (Number.class.isAssignableFrom(type))
             try {
               value = Classes.valueOf(type, property);
             } catch (NumberFormatException e) {
               unexpectedException(e);
               continue;
             }
         }
         final Object old;
         try {
           old = hints.put(hintKey, value);
         } catch (IllegalArgumentException e) {
           // The property value is illegal for this hint.
           unexpectedException(e);
           continue;
         }
         if (!changed && !Utilities.equals(old, value)) {
           changed = true;
         }
       }
     }
   }
   return changed;
 }
 /**
  * Look for additional dimensions in the dimensionsHelper and put additional domains to the
  * metadata
  *
  * @param helper
  * @throws IOException
  */
 private void handleAdditionalDimensionMetadata(final WCSDimensionsHelper helper)
     throws IOException {
   Utilities.ensureNonNull("helper", helper);
   final Map<String, DimensionInfo> additionalDimensions = helper.getAdditionalDimensions();
   final Set<String> dimensionsName = additionalDimensions.keySet();
   final Iterator<String> dimensionsIterator = dimensionsName.iterator();
   while (dimensionsIterator.hasNext()) {
     final String dimensionName = dimensionsIterator.next();
     final DimensionInfo customDimension = additionalDimensions.get(dimensionName);
     if (customDimension != null) {
       setAdditionalDimensionMetadata(dimensionName, customDimension, helper);
     }
   }
 }
    /**
     * Set additional dimension metadata to the DescribeCoverage element
     *
     * @param name the custom dimension name
     * @param dimension the custom dimension related {@link DimensionInfo} instance
     * @param helper the {@link WCSDimensionsHelper} instance to be used to parse domains
     * @throws IOException
     */
    private void setAdditionalDimensionMetadata(
        final String name, final DimensionInfo dimension, WCSDimensionsHelper helper)
        throws IOException {
      Utilities.ensureNonNull("helper", helper);
      final String startTag =
          initStartMetadataTag(TAG.ADDITIONAL_DIMENSION, name, dimension, helper);

      start(startTag);
      // Custom dimension only supports List presentation
      final List<String> domain = helper.getDomain(name);
      // TODO: check if we are in the list of instants case, or in the list of periods case

      // list case
      int i = 0;
      for (String item : domain) {
        Date date = WCSDimensionsValueParser.parseAsDate(item);
        if (date != null) {
          final String dimensionId = helper.getCoverageId() + "_dd_" + i;
          encodeDate(date, helper, dimensionId);
          continue;
        }

        Double number = WCSDimensionsValueParser.parseAsDouble(item);
        if (number != null) {
          element(TAG.SINGLE_VALUE, item.toString());
          continue;
        }

        NumberRange<Double> range = WCSDimensionsValueParser.parseAsDoubleRange(item);
        if (range != null) {
          encodeInterval(
              range.getMinValue().toString(), range.getMaxValue().toString(), null, null);
          continue;
        }

        // TODO: Add support for date Ranges
        if (item instanceof String) {
          element(TAG.SINGLE_VALUE, item.toString());
        }
        //                else if (item instanceof DateRange) {
        //                    final String dimensionId = helper.getCoverageId() + "_dd_" + i;
        //                    encodeDateRange((DateRange) item, helper, dimensionId);
        //                }

        // TODO: Add more cases
        i++;
      }
      end(TAG.ADDITIONAL_DIMENSION);
    }
  /**
   * Extract the ImageLayout from the provided reader for the first available image.
   *
   * @param reader an istance of {@link ImageReader}
   * @throws IOException in case an error occurs
   */
  protected void setLayout(ImageReader reader) throws IOException {

    Utilities.ensureNonNull("reader", reader);
    // save ImageLayout
    ImageLayout2 layout = new ImageLayout2();
    ImageTypeSpecifier its = reader.getImageTypes(0).next();
    layout.setColorModel(its.getColorModel()).setSampleModel(its.getSampleModel());
    layout.setMinX(0).setMinY(0).setWidth(reader.getWidth(0)).setHeight(reader.getHeight(0));
    layout
        .setTileGridXOffset(0)
        .setTileGridYOffset(0)
        .setTileWidth(reader.getTileWidth(0))
        .setTileHeight(reader.getTileHeight(0));
    setlayout(layout);
  }
    /**
     * Set the timeDomain metadata in case the dimensionsHelper instance has a timeDimension
     *
     * @param helper
     * @throws IOException
     */
    private void handleTimeMetadata(WCSDimensionsHelper helper) throws IOException {
      Utilities.ensureNonNull("helper", helper);
      final DimensionInfo timeDimension = helper.getTimeDimension();
      if (timeDimension != null) {
        start(initStartMetadataTag(TAG.TIME_DOMAIN, null, timeDimension, helper));
        final DimensionPresentation presentation = timeDimension.getPresentation();
        final String id = helper.getCoverageId();
        switch (presentation) {
          case CONTINUOUS_INTERVAL:
            encodeTimePeriod(helper.getBeginTime(), helper.getEndTime(), id + "_tp_0", null, null);
            break;
          case DISCRETE_INTERVAL:
            encodeTimePeriod(
                helper.getBeginTime(),
                helper.getEndTime(),
                id + "_tp_0",
                helper.getTimeResolutionUnit(),
                helper.getTimeResolutionValue());
            break;
          default:
            // TODO: check if we are in the list of instants case, or in the list of periods case

            // list case
            final TreeSet<Object> domain = helper.getTimeDomain();
            int i = 0;
            for (Object item : domain) {
              // gml:id is mandatory for time instant...
              if (item instanceof Date) {
                encodeDate((Date) item, helper, id + "_td_" + i);
              } else if (item instanceof DateRange) {
                encodeDateRange((DateRange) item, helper, id + "_td_" + i);
              }
              i++;
            }
            break;
        }
        end(TAG.TIME_DOMAIN);
      }
    }
 public void setRequestedGridGeometry(GridGeometry2D gridGeometry) {
   Utilities.ensureNonNull("girdGeometry", gridGeometry);
   requestedBBox = new ReferencedEnvelope((Envelope) gridGeometry.getEnvelope2D());
   requestedRasterArea = gridGeometry.getGridRange2D().getBounds();
   requestedGridToWorld = (AffineTransform) gridGeometry.getGridToCRS2D();
 }
 /** Computes the hash value for this reader/input pair. */
 private int hash() {
   return reader.hashCode() + 37 * Utilities.deepHashCode(input);
 }
 /**
  * This method is responsible for checking the provided coverage name against the coverage name
  * for this {@link GridCoverage2DReader}.
  *
  * @param coverageName the coverage name to check.
  * @return <code>true</code> if this {@link GridCoverage2DReader} contains the provided coverage
  *     name, <code>false</code> otherwise.
  */
 protected boolean checkName(String coverageName) {
   Utilities.ensureNonNull("coverageName", coverageName);
   return coverageName.equalsIgnoreCase(this.coverageName);
 }
 /**
  * Set the provided layout for this {@link GridCoverage2DReader}-
  *
  * @param layout the {@link ImageLayout} to set. It must be nont null
  */
 protected void setlayout(ImageLayout layout) {
   Utilities.ensureNonNull("layout", layout);
   this.imageLayout = (ImageLayout) layout.clone();
 }
  public static SimpleFeatureType createSubType(
      SimpleFeatureType featureType,
      String[] properties,
      CoordinateReferenceSystem override,
      String typeName,
      URI namespace)
      throws SchemaException {

    if ((properties == null) && (override == null)) {
      return featureType;
    }

    if (properties == null) {
      properties = new String[featureType.getAttributeCount()];
      for (int i = 0; i < properties.length; i++) {
        properties[i] = featureType.getDescriptor(i).getLocalName();
      }
    }

    String namespaceURI = namespace != null ? namespace.toString() : null;
    boolean same =
        featureType.getAttributeCount() == properties.length
            && featureType.getTypeName().equals(typeName)
            && Utilities.equals(featureType.getName().getNamespaceURI(), namespaceURI);

    for (int i = 0; (i < featureType.getAttributeCount()) && same; i++) {
      AttributeDescriptor type = featureType.getDescriptor(i);
      same =
          type.getLocalName().equals(properties[i])
              && (((override != null) && type instanceof GeometryDescriptor)
                  ? assertEquals(
                      override, ((GeometryDescriptor) type).getCoordinateReferenceSystem())
                  : true);
    }

    if (same) {
      return featureType;
    }

    AttributeDescriptor[] types = new AttributeDescriptor[properties.length];

    for (int i = 0; i < properties.length; i++) {
      types[i] = featureType.getDescriptor(properties[i]);

      if ((override != null) && types[i] instanceof GeometryDescriptor) {
        AttributeTypeBuilder ab = new AttributeTypeBuilder();
        ab.init(types[i]);
        ab.setCRS(override);
        types[i] = ab.buildDescriptor(types[i].getLocalName(), ab.buildGeometryType());
      }
    }

    if (typeName == null) typeName = featureType.getTypeName();
    if (namespace == null && featureType.getName().getNamespaceURI() != null)
      try {
        namespace = new URI(featureType.getName().getNamespaceURI());
      } catch (URISyntaxException e) {
        throw new RuntimeException(e);
      }

    SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
    tb.setName(typeName);
    tb.setNamespaceURI(namespace);
    tb.addAll(types);

    return tb.buildFeatureType();
  }