org.apache.sis.math.Vector.create()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(86)

本文整理了Java中org.apache.sis.math.Vector.create()方法的一些代码示例,展示了Vector.create()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vector.create()方法的具体详情如下:
包路径:org.apache.sis.math.Vector
类名称:Vector
方法名:create

Vector.create介绍

[英]Wraps the given object in a vector. The argument should be one of the following:

  • An array of a primitive type, like float[].
  • A Number[] array.
  • A String[] array (not recommended, but happen with some file formats).
  • A Vector, in which case it is returned unchanged.
  • A Buffer backed by an array.
  • The null value, in which case null is returned.
    The given argument is not cloned. Consequently changes in the underlying array are reflected in this vector, and vice-versa.

Unsigned integers
Java has no primitive support for unsigned integers. But some file formats use unsigned integers, which can be simulated in Java by the use of bit masks or methods like Integer#toUnsignedLong(int). This Vector class applies automatically those masks (unless otherwise noticed in method Javadoc) if the isUnsigned argument is true. That argument applies only to byte[], short[], int[] or long[] arrays and is ignored for all other kind of arrays.
[中]将给定对象包裹在向量中。论点应该是以下之一:
*基本类型的数组,如float[]。
*数字[]数组。
*字符串[]数组(不推荐,但在某些文件格式中会出现)。
*一个向量,在这种情况下,它会原封不动地返回。
*由数组支持的缓冲区。
*null值,在这种情况下返回null。
给定的参数不是克隆的。因此,底层数组中的变化会反映在该向量中,反之亦然。
无符号整数
Java不支持无符号整数。但有些文件格式使用无符号整数,可以通过使用位掩码或Integer#toUnsignedLong(int)等方法在Java中模拟无符号整数。如果isUnsigned参数为true,这个向量类会自动应用这些掩码(除非在方法Javadoc中另有说明)。该参数仅适用于字节[]、短[]、int[]或长[]数组,对于所有其他类型的数组都被忽略。

代码示例

代码示例来源:origin: apache/sis

/**
 * Wraps the given data in a {@link Vector} with the assumption that accuracy in base 10 matters.
 * This method is suitable for coordinate axis variables, but should not be used for the main data.
 *
 * @param  data        the data to wrap in a vector.
 * @param  isUnsigned  whether the data type is an unsigned type.
 * @return vector wrapping the given data.
 */
protected static Vector createDecimalVector(final Object data, final boolean isUnsigned) {
  if (data instanceof float[]) {
    return Vector.createForDecimal((float[]) data);
  } else {
    return Vector.create(data, isUnsigned);
  }
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
 * Wraps the given array in a vector of length {@link #numPoints}. This method should be
 * invoked only when this builder has been created by {@link #LinearTransformBuilder()}.
 * This can be identified by {@code sources != null} or {@code gridSize == null}.
 */
private Vector vector(final double[] data) {
  assert gridSize == null;
  return Vector.create(data, false).subList(0, numPoints);
}

代码示例来源:origin: org.apache.sis.core/sis-feature

/**
 * Creates a new list for the given times.
 */
DateList(final long[] millis) {
  times = Vector.create(millis, false).compress(0);
}

代码示例来源:origin: apache/sis

/**
 * Creates a new list for the given times.
 */
DateList(final long[] millis) {
  times = Vector.create(millis, false).compress(0);
}

代码示例来源:origin: apache/sis

/**
 * Wraps the given array in a vector of length {@link #numPoints}. This method should be
 * invoked only when this builder has been created by {@link #LinearTransformBuilder()}.
 * This can be identified by {@code sources != null} or {@code gridSize == null}.
 */
private Vector vector(final double[] data) {
  assert gridSize == null;
  return Vector.create(data, false).subList(0, numPoints);
}

代码示例来源:origin: apache/sis

/**
   * Reads an arbitrary amount of values as a wrapper around a Java array of primitive type.
   * This wrapper provide a more convenient way to access array elements than the object
   * returned by {@link #readArray(ChannelDataInput, int)}.
   *
   * @param  input  the input from where to read the values.
   * @param  count  the amount of values.
   * @return the value as a wrapper around a Java array of primitive type.
   * @throws IOException if an error occurred while reading the stream.
   * @throws ArithmeticException if the given count is too large.
   * @throws NumberFormatException if the value was stored in ASCII and can not be parsed.
   * @throws UnsupportedOperationException if this type is {@link #UNDEFINED}.
   */
  public final Vector readVector(final ChannelDataInput input, final long count) throws IOException {
    return Vector.create(readArray(input, Math.toIntExact(count)), isUnsigned);
  }
}

代码示例来源:origin: org.apache.sis.storage/sis-netcdf

/**
 * Reads all the data for this variable and returns them as an array of a Java primitive type.
 * Multi-dimensional variables are flattened as a one-dimensional array (wrapped in a vector).
 * This method may cache the returned vector, at UCAR library choice.
 */
@Override
public Vector read() throws IOException {
  final Array array = variable.read();                // May be cached by the UCAR library.
  return Vector.create(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link Vector#reverse()}.
 */
@Test
@DependsOnMethod("testDoubleArray")
public void testReverse() {
  final double[] array    = {2, 3, 8};
  final double[] expected = {8, 3, 2};
  assertEquals(Vector.create(expected, false),
         Vector.create(array, false).reverse());
}

代码示例来源:origin: Geomatys/geotoolkit

/**
 * Tests the {@link VectorPair#omitColinearPoints} method.
 */
@Test
public void testOmitColinearPoints() {
  final double[]  x = {1, 2, 3, 4, 5, 6, 7, 8};
  final double[]  y = {4, 4, 3, 3, 3, 4, 5, 6};
  final double[] ey = {4, 4, 3,    3,       6}; // Expected result
  final double[] ex = {1, 2, 3,    5,       8};
  final VectorPair pair = new VectorPair(Vector.create(x, false),
                      Vector.create(y, false));
  pair.omitColinearPoints(1E-6, 1E-6);
  assertEquals(Vector.create(ey, false), pair.getY());
  assertEquals(Vector.create(ex, false), pair.getX());
}

代码示例来源:origin: Geomatys/geotoolkit

/**
 * Tests the {@link VectorPair#makeStepwise} method.
 */
@Test
public void testMakeStepwise() {
  final double[]  x = {1, 2, 3, 4, 5, 6, 7};
  final double[]  y = {4, 5, 6, 6, 3, 4};
  final double[] ey = {4, 4, 5, 5, 6, 6, 6, 6, 3, 3, 4, 4};
  final double[] ex = {1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7};
  final VectorPair pair = new VectorPair(Vector.create(x, false),
                      Vector.create(y, false));
  pair.makeStepwise(0);
  assertEquals(Vector.create(ey, false), pair.getY());
  assertEquals(Vector.create(ex, false), pair.getX());
}

代码示例来源:origin: apache/sis

/**
 * Tests the {@link Vector#toString()} method on a vector of signed and unsigned bytes.
 */
@Test
public void testToString() {
  final byte[] array = new byte[] {(byte) 10, (byte) 100, (byte) 200};
  vector = Vector.create(array, true);
  assertEquals("[10, 100, 200]", vector.toString());
  vector = Vector.create(array, false);
  assertEquals("[10, 100, -56]", vector.toString());
}

代码示例来源:origin: apache/sis

/**
 * Reads a sub-sampled sub-area of the variable.
 * Array elements are in inverse of netCDF order.
 *
 * @param  area         indices of cell values to read along each dimension, in "natural" order.
 * @param  subsampling  sub-sampling along each dimension. 1 means no sub-sampling.
 * @return the data as an array of a Java primitive type.
 */
@Override
public Vector read(final GridExtent area, final int[] subsampling) throws IOException, DataStoreException {
  int n = area.getDimension();
  final int[] lower = new int[n];
  final int[] size  = new int[n];
  final int[] sub   = new int[n--];
  for (int i=0; i<=n; i++) {
    final int j = (n - i);
    lower[j] = Math.toIntExact(area.getLow(i));
    size [j] = Math.toIntExact(area.getSize(i));
    sub  [j] = subsampling[i];
  }
  final Array array;
  try {
    array = variable.read(new Section(lower, size, sub));
  } catch (InvalidRangeException e) {
    throw new DataStoreException(e);
  }
  return Vector.create(get1DJavaArray(array), variable.isUnsigned());
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link Vector#repetitions(int...)}.
 */
@Test
public void testRepetitions() {
  Vector vec = Vector.create(new int[] {
      10, 10, 10, 10,
      12, 12, 13, 12,             // Different value (13) break the regularity.
      15, 15, 15, 15}, false);
  assertArrayEquals(new int[] {}, vec.repetitions());
  vec = Vector.create(new int[] {
      10, 10, 10, 10,
      12, 12, 12, 12,
      15, 15, 15, 15}, false);
  assertArrayEquals(new int[] {4}, vec.repetitions());
  vec = Vector.create(new int[] {
      10, 10, 10,  12, 12, 12,  15, 15, 15,  18, 18, 18,
      10, 10, 10,  12, 12, 12,  15, 15, 15,  18, 18, 18,
      10, 10, 10,  12, 12, 12,  15, 15, 15,  18, 18, 18}, false);
  assertArrayEquals(new int[] {3,4}, vec.repetitions());
  vec = Vector.create(new int[] {
      10, 12, 15, 18,
      10, 12, 15, 18,
      10, 12, 15, 18}, false);
  assertArrayEquals(new int[] {1,4}, vec.repetitions());
}

代码示例来源:origin: org.apache.sis.storage/sis-netcdf

/**
   * Reads a sub-sampled sub-area of the variable.
   *
   * @param  areaLower    index of the first value to read along each dimension.
   * @param  areaUpper    index after the last value to read along each dimension.
   * @param  subsampling  sub-sampling along each dimension. 1 means no sub-sampling.
   * @return the data as an array of a Java primitive type.
   */
  @Override
  public Vector read(final int[] areaLower, final int[] areaUpper, final int[] subsampling)
      throws IOException, DataStoreException
  {
    final int[] size = new int[areaUpper.length];
    for (int i=0; i<size.length; i++) {
      size[i] = areaUpper[i] - areaLower[i];
    }
    final Array array;
    try {
      array = variable.read(new Section(areaLower, size, subsampling));
    } catch (InvalidRangeException e) {
      throw new DataStoreContentException(e);
    }
    return Vector.create(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
  }
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link ArrayVector} backed by an array of double type.
 */
@Test
public void testDoubleArray() {
  final double[] array = new double[400];
  for (int i=0; i<array.length; i++) {
    array[i] = (i + 100) * 10;
  }
  vector = Vector.create(array, false);
  assertEquals("Doubles", vector.getClass().getSimpleName());
  assertSame(vector, Vector.create(vector, false));
  assertEquals(array.length, vector.size());
  assertEquals(Double.class, vector.getElementType());
  /*
   * Tests element values.
   */
  for (int i=0; i<array.length; i++) {
    assertEquals(array[i], vector.floatValue (i), 0f);
    assertEquals(array[i], vector.doubleValue(i), STRICT);
  }
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link ArrayVector} backed by an array of float type.
 */
@Test
public void testFloatArray() {
  final float[] array = new float[400];
  for (int i=0; i<array.length; i++) {
    array[i] = (i + 100) * 10;
  }
  vector = Vector.create(array, false);
  assertEquals("Floats", vector.getClass().getSimpleName());
  assertSame(vector, Vector.create(vector, false));
  assertEquals(array.length, vector.size());
  assertEquals(Float.class, vector.getElementType());
  /*
   * Tests element values.
   */
  for (int i=0; i<array.length; i++) {
    assertEquals(array[i], vector.floatValue (i), 0f);
    assertEquals(array[i], vector.doubleValue(i), STRICT);
  }
}

代码示例来源:origin: apache/sis

/**
 * Tests a vector backed by an array of strings.
 * This is not recommended, but happen in GDAL extensions of GeoTIFF.
 * See {@code org.apache.sis.storage.geotiff.Type.ASCII}.
 */
@Test
public void testStringArray() {
  vector = Vector.create(new String[] {"100", "80", "-20"}, false);
  assertEquals(  3, vector.size());
  assertEquals(100, vector.intValue(0));
  assertEquals( 80, vector.shortValue(1));
  assertEquals(-20, vector.doubleValue(2), STRICT);
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link Geometries#createPolyline(int, Vector...)}.
 * This method verifies the polylines by a call to {@link Geometries#tryGetEnvelope(Object)}.
 * Subclasses should perform more extensive tests by verifying the {@link #geometry} field.
 */
@Test
public void testCreatePolyline() {
  geometry = factory.createPolyline(2, Vector.create(new double[] {
       4,   5,
       7,   9,
       9,   3,
       4,   5,
      NaN, NaN,
       -3,  -2,
       -2,  -5,
       -1,  -6}, false));
  final GeneralEnvelope env = factory.tryGetEnvelope(geometry);
  assertEquals("xmin", -3, env.getLower(0), STRICT);
  assertEquals("ymin", -6, env.getLower(1), STRICT);
  assertEquals("xmax",  9, env.getUpper(0), STRICT);
  assertEquals("ymax",  9, env.getUpper(1), STRICT);
}

代码示例来源:origin: apache/sis

/**
 * Tests the case where values in a grid are repeated horizontally.
 */
@Test
public void testHorizontal() {
  Vector vec = Vector.create(new int[] {
      10, 10, 10, 10,
      12, 12, 12, 12,
      15, 15, 15, 15}, false);
  vec = new RepeatedVector(vec, vec.repetitions(), 0);
  assertArrayEquals(new int[] {4}, vec.repetitions());
  assertEquals(10, vec.intValue  ( 0));
  assertEquals(10, vec.shortValue( 1));
  assertEquals(10, vec.longValue ( 2));
  assertEquals(10, vec.intValue  ( 3));
  assertEquals(12, vec.intValue  ( 4));
  assertEquals(12, vec.shortValue( 7));
  assertEquals(15, vec.longValue ( 8));
  assertEquals(15, vec.intValue  (11));
  Vector sub = vec.subSampling(0, 4, 3);
  assertFalse("Expected the backing array.", sub instanceof RepeatedVector);
  assertArrayEquals(new float[] {10, 12, 15}, sub.floatValues(), (float) STRICT);
}

代码示例来源:origin: apache/sis

/**
 * Tests the case where values in a grid are repeated vertically.
 */
@Test
public void testVertical() {
  Vector vec = Vector.create(new int[] {
      10, 12, 15, 18,
      10, 12, 15, 18,
      10, 12, 15, 18}, false);
  vec = new RepeatedVector(vec, vec.repetitions(), 0);
  assertArrayEquals(new int[] {1,4}, vec.repetitions());
  assertEquals(10, vec.intValue  ( 0));
  assertEquals(12, vec.shortValue( 1));
  assertEquals(15, vec.longValue ( 2));
  assertEquals(18, vec.intValue  ( 3));
  assertEquals(10, vec.intValue  ( 4));
  assertEquals(18, vec.shortValue( 7));
  assertEquals(10, vec.longValue ( 8));
  assertEquals(15, vec.intValue  (10));
  Vector sub = vec.subList(0, 4);
  assertFalse("Expected the backing array.", sub instanceof RepeatedVector);
  assertArrayEquals(new float[] {10, 12, 15, 18}, sub.floatValues(), (float) STRICT);
}

相关文章