Example #1
0
  private String getContent(String name) throws Exception {
    DataSource ds = dsf.getDataSource(name);
    ds.open();
    String ret = ds.getAsString();
    ds.close();

    return ret;
  }
Example #2
0
 public void testProducedRasterEnvelope() throws Exception {
   DataSource ds = dsf.getDataSource("raster");
   ds.open();
   SpatialDataSourceDecorator sds = new SpatialDataSourceDecorator(ds);
   Envelope env = sds.getFullExtent();
   assertTrue(env.getWidth() > 0);
   assertTrue(env.getHeight() > 0);
   ds.close();
 }
Example #3
0
  @Test
  public void testSameSourceSameDSInstance() throws Exception {
    sm.register(SOURCE, testFile);

    DataSource ds1 = dsf.getDataSource(SOURCE, DataSourceFactory.NORMAL);
    DataSource ds2 = dsf.getDataSource(SOURCE, DataSourceFactory.NORMAL);
    ds1.open();
    assertTrue(ds2.isOpen());
    ds2.close();
  }
Example #4
0
  @Test
  public void testDependingNotWellKnownSourcesRemoved() throws Exception {
    sm.register(SOURCE, testFile);
    DataSource ds = dsf.getDataSourceFromSQL("select * from " + SOURCE + ";");
    assertEquals(ds.getReferencedSources().length, 1);

    String nwkn = ds.getName();
    dsf.getSourceManager().remove(SOURCE);
    assertFalse(dsf.getSourceManager().exists(nwkn));
  }
Example #5
0
 @Override
 protected void checkActionState() {
   super.checkActionState();
   // Active only if the DataSource is modified
   if (ActionTools.isVisible(this)) {
     DataSource dataSource =
         getExtension().getMapElement().getMapContext().getActiveLayer().getDataSource();
     setEnabled(dataSource != null && dataSource.canUndo());
   }
 }
Example #6
0
  private String associateString(Source source, String propertyName) throws Exception {
    DataSource ds = dsf.getDataSource(SOURCE);
    ds.open();
    long rc = ds.getRowCount();
    ds.close();

    String rcStr = Long.toString(rc);
    source.putProperty(propertyName, rcStr);
    return rcStr;
  }
Example #7
0
  @Test
  public void testSaveWithAnOpenHSQLDBDataSource() throws Exception {
    Assume.assumeTrue(hsqlDbAvailable);

    sm.register("db", testDB);
    DataSource ds = dsf.getDataSource("db");
    ds.open();
    sm.saveStatus();
    ds.getFieldValue(0, 0);
    ds.close();
  }
Example #8
0
 private void testOpen(File file) throws Exception {
   GeoRaster gr = GeoRasterFactory.createGeoRaster(file.getAbsolutePath());
   gr.open();
   int rasterType = gr.getType();
   DataSource ds = dsf.getDataSource(file);
   ds.open();
   Metadata metadata = ds.getMetadata();
   Type fieldType = metadata.getFieldType(0);
   assertTrue(fieldType.getIntConstraint(Constraint.RASTER_TYPE) == rasterType);
   ds.getFieldValue(0, 0);
   ds.close();
 }
  private void testIndexRealData(String source, double checkPeriod, DiskRTree tree)
      throws NoSuchTableException, DataSourceCreationException, DriverException, IOException,
          Exception {
    DataSource ds = dsf.getDataSource(source);
    String fieldName = "the_geom";

    ds.open();
    int fieldIndex = ds.getFieldIndexByName(fieldName);
    for (int i = 0; i < ds.getRowCount(); i++) {
      if (i / (int) checkPeriod == i / checkPeriod) {
        tree.checkTree();
        tree.close();
        tree.openIndex(indexFile);
        tree.checkTree();
        checkLookUp(tree, ds, fieldIndex);
      }
      Envelope value = ds.getFieldValue(i, fieldIndex).getAsGeometry().getEnvelopeInternal();
      tree.insert(value, i);
    }
    for (int i = 0; i < ds.getRowCount(); i++) {
      if (i / (int) checkPeriod == i / checkPeriod) {
        tree.checkTree();
        tree.save();
        tree.checkTree();
        checkLookUp(tree, ds, fieldIndex);
      }
      Value value = ds.getFieldValue(i, fieldIndex);
      tree.delete(value.getAsGeometry().getEnvelopeInternal(), i);
    }

    ds.close();
  }
 /**
  * @param value String representation of the field
  * @return The corresponding value or null value if constraints are not respected.
  * @throws ParseException Unable to parse provided string
  * @throws DriverException The driver throw this exception
  */
 public Value getValue(Object value) throws ParseException, DriverException {
   Type fieldType = ds.getMetadata().getFieldType(fieldIndex);
   Value inputValue;
   if (value != null && (fieldType.getTypeCode() == Type.STRING || !value.toString().isEmpty())) {
     inputValue = ValueFactory.createValueByType(value.toString(), fieldType.getTypeCode());
   } else {
     inputValue = ValueFactory.createNullValue();
   }
   String error = ds.check(fieldIndex, inputValue);
   if (error == null || error.isEmpty()) {
     return inputValue;
   } else {
     throw new ParseException(error, 0);
   }
 }
Example #11
0
 @Test
 public void testRegisterTwice() throws Exception {
   sm.register(SOURCE, getTempFile(".shp"));
   try {
     sm.register(SOURCE, getTempFile(".shp"));
     fail();
   } catch (SourceAlreadyExistsException e) {
     // we check that the failed registration has broken nothing
     sm.remove(SOURCE);
     sm.register(SOURCE, testFile);
     DataSource ds = dsf.getDataSource(SOURCE);
     ds.open();
     ds.close();
   }
 }
Example #12
0
  private String associateFile(Source source, String propertyName) throws Exception {
    if (source.hasProperty(propertyName)) {
      source.deleteProperty(propertyName);
    }
    File stats = source.createFileProperty(propertyName);
    DataSource ds = dsf.getDataSource(source.getName());
    ds.open();
    long rc = ds.getRowCount();
    ds.close();

    FileOutputStream fis = new FileOutputStream(stats);
    String rcStr = Long.toString(rc);
    fis.write(rcStr.getBytes());
    fis.close();

    return rcStr;
  }
Example #13
0
  @Test
  public void testModifyField() throws Exception {
    sm.register("big", getTempCopyOf(getAnyNonSpatialResource()));
    DataSource d = dsf.getDataSource("big");

    d.open();
    d.getMetadata();
    d.setFieldName(1, "nuevo");
    d.commit();
    d.close();
    d.open();
    assertEquals(d.getMetadata().getFieldName(1), "nuevo");
    d.close();
  }
 public boolean execute(PlugInContext context) throws Exception {
   IEditor editor = getPlugInContext().getActiveEditor();
   MapContext map = (MapContext) editor.getElement().getObject();
   ILayer activeLayer = map.getActiveLayer();
   int[] sel = activeLayer.getSelection().clone();
   Arrays.sort(sel);
   DataSource dataSource = activeLayer.getDataSource();
   try {
     dataSource.setDispatchingMode(DataSource.STORE);
     for (int i = sel.length - 1; i >= 0; i--) {
       dataSource.deleteRow(sel[i]);
     }
     dataSource.setDispatchingMode(DataSource.DISPATCH);
   } catch (DriverException e) {
     ErrorMessages.error(ErrorMessages.CannotDeleteSelectedRow, e);
   }
   return true;
 }
 @Override
 public void visitElement(int row, Envelope env) {
   fired = true;
   try {
     assertTrue(env.contains(ds.getGeometry(row).getEnvelopeInternal()));
   } catch (DriverException ex) {
     fail();
   }
 }
  @Override
  protected void rectangleDone(
      Rectangle2D rect, boolean smallerThanTolerance, MapContext vc, ToolManager tm)
      throws TransitionException {
    ILayer layer = vc.getSelectedLayers()[0];
    DataSource sds = layer.getDataSource();
    String sql = null;
    try {
      GeometryFactory gf = ToolManager.toolsGeometryFactory;
      double minx = rect.getMinX();
      double miny = rect.getMinY();
      double maxx = rect.getMaxX();
      double maxy = rect.getMaxY();

      Coordinate lowerLeft = new Coordinate(minx, miny);
      Coordinate upperRight = new Coordinate(maxx, maxy);
      LinearRing envelopeShell =
          gf.createLinearRing(
              new Coordinate[] {
                lowerLeft,
                new Coordinate(minx, maxy),
                upperRight,
                new Coordinate(maxx, miny),
                lowerLeft,
              });
      Geometry geomEnvelope = gf.createPolygon(envelopeShell, new LinearRing[0]);
      WKTWriter writer = new WKTWriter();
      sql =
          "select * from "
              + layer.getName()
              + " where ST_intersects("
              + sds.getMetadata().getFieldName(sds.getSpatialFieldIndex())
              + ", ST_geomfromtext('"
              + writer.write(geomEnvelope)
              + "'));";
      BackgroundManager bm = (BackgroundManager) Services.getService(BackgroundManager.class);
      bm.backgroundOperation(
          new DefaultJobId("org.orbisgis.jobs.InfoTool"), new PopulateViewJob(sql));
    } catch (DriverException e) {
      throw new TransitionException(e);
    } catch (DriverLoadException e) {
      throw new RuntimeException(e);
    }
  }
Example #17
0
  @Test
  public void testSelectDependencies() throws Exception {
    sm.register("file", testFile);
    String sql = "select 2*(file.id :: int) from file " + "where (file.id :: int) <> 234;";
    sm.register("sql", sql);
    DataSource ds = dsf.getDataSource("sql");
    assertTrue(setIs(ds.getReferencedSources(), new String[] {"file"}));
    ds = dsf.getDataSourceFromSQL(sql);
    assertTrue(setIs(ds.getReferencedSources(), new String[] {"file"}));
    sql = "select * from file union select * from file;";
    sm.register("sql2", sql);
    ds = dsf.getDataSource("sql2");
    assertTrue(setIs(ds.getReferencedSources(), new String[] {"file"}));
    ds = dsf.getDataSourceFromSQL(sql);
    assertTrue(setIs(ds.getReferencedSources(), new String[] {"file"}));

    String[] srcDeps = dsf.getDataSource("file").getReferencedSources();
    assertEquals(0, srcDeps.length);
  }
    @Override
    public void sourceAdded(SourceEvent e) {
      String name = e.getName();
      SourceManager sm = dsf.getSourceManager();
      if (e.isWellKnownName()
          && !sm.getSource(name).isSystemTableSource()
          && !layerMap.containsKey(name)) {
        try {
          Layer layer = new Layer();
          layer.setName(name);
          layer.setTitle(name);

          // Setting the bounding box data
          DataSource ds = dsf.getDataSource(name);
          ds.open();
          Envelope env = ds.getFullExtent();
          CoordinateReferenceSystem crs = ds.getCRS();
          ds.close();
          BoundingBox bBox = getBoundingBox(env, crs);
          if (bBox == null) {
            return;
          }
          layer.getCRS().add(bBox.getCRS());
          layer.getBoundingBox().add(bBox);
          layer.setEXGeographicBoundingBox(getGeographicBoundingBox(env, bBox.getCRS()));
          layer.setQueryable(true);
          if (layerStyles.containsKey(name)) {
            String[] lStyles = layerStyles.get(name);
            for (int i = 0; i < lStyles.length; i++) {
              Style style = new Style();
              String styleName = lStyles[i];
              style.setName(styleName);
              style.setTitle(styleName);
              layer.getStyle().add(style);
            }
          }
          layerMap.put(name, layer);
        } catch (NoSuchTableException ex) {
        } catch (DataSourceCreationException ex) {
        } catch (DriverException ex) {
        }
      }
    }
  private boolean contains(int[] indexes, DataSource ds, int fieldIndex, Envelope geometry)
      throws Exception {
    for (int i : indexes) {
      if (ds.getFieldValue(i, fieldIndex).getAsGeometry().getEnvelopeInternal().equals(geometry)) {
        return true;
      }
    }

    return false;
  }
Example #20
0
  @Test
  public void testFieldIndexDataSource() throws Exception {

    DataSource ds = dsf.getDataSource(super.getAnyNonSpatialResource());
    ds.open();
    assertEquals(
        ds.getMetadata().getFieldIndex(ds.getMetadata().getFieldName(1)),
        ds.getFieldIndexByName(ds.getFieldName(1)));
    ds.close();
  }
Example #21
0
  @Test
  public void testGetAlreadyRegisteredSourceAnonimously() throws Exception {
    sm.register("myfile", testFile);
    if (hsqlDbAvailable) {
      sm.register("myDB", testDB);
    }
    sm.register("myWMS", testWMS);
    sm.register("myObj", obj);

    DataSource ds = dsf.getDataSource(testFile);
    assertEquals(ds.getName(), "myfile");

    if (hsqlDbAvailable) {
      ds = dsf.getDataSource(testDB);
      assertEquals(ds.getName(), "myDB");
    }

    ds = dsf.getDataSource(testWMS);
    assertEquals(ds.getName(), "myWMS");

    ds = dsf.getDataSource(obj, "main");
    assertEquals(ds.getName(), "myObj");

    sm.removeAll();
    sm.register("myfile", super.getAnySpatialResource());
    sm.register("mySQL", sql);

    ds = dsf.getDataSourceFromSQL(sql);
    assertEquals(ds.getName(), "mySQL");
  }
Example #22
0
  @Test
  public void testDeleteField() throws Exception {
    sm.register(
        "big", getTempCopyOf(new File(TestResourceHandler.TESTRESOURCES, "landcover2000.shp")));
    DataSource d = dsf.getDataSource("big");

    d.open();
    Metadata m = d.getMetadata();
    int fc = m.getFieldCount();
    d.removeField(1);
    d.commit();
    d.close();
    d.open();
    assertEquals(fc - 1, m.getFieldCount());
    d.close();
  }
Example #23
0
 /**
  * Adds the symbol panel attached to the given {@link Symbolizer} and returns the panel.
  *
  * @param symb Symbolizer
  * @return The newly generated symbol panel
  */
 private ILegendPanel addSymbolPanel(Symbolizer symb) {
   // Get the legend corresponding to this symbolizer.
   Legend legend = LegendFactory.getLegend(symb);
   if (legend instanceof AbstractRecodedLegend) {
     DataSource dataSource = layer.getDataSource();
     AbstractRecodedLegend leg = (AbstractRecodedLegend) legend;
     try {
       Metadata metadata = dataSource.getMetadata();
       String f = leg.getLookupFieldName();
       int in = metadata.getFieldIndex(f);
       leg.setComparator(AbstractRecodedLegend.getComparator(metadata.getFieldType(in)));
     } catch (DriverException e) {
       LOGGER.warn("Can't retrieve an accurate Comparator for this classification");
     }
   }
   // Initialize a panel for this legend.
   ILegendPanel panel = ILegendPanelFactory.getILegendPanel(this, legend);
   // Give it a new id.
   panel.setId(createNewID());
   // Add the symbol panel to the container after putting it in a
   // new JScrollPane.
   dialogContainer.add(panel.getId(), getJScrollPane(panel));
   return panel;
 }
  @Test
  public void testGetSetCRS() throws Exception {
    dsf.executeSQL("CREATE TABLE init AS SELECT * FROM ST_RandomGeometry('point', 10);");
    DataSource ds = dsf.getDataSourceFromSQL("SELECT ST_CRS(the_geom) from init;");

    ds.open();
    assertTrue(ds.isNull(0, 0));
    ds.close();

    ds = dsf.getDataSourceFromSQL("SELECT ST_CRS(ST_SetCRS(the_geom, 'EPSG:27572')) from init;");

    ds.open();
    for (int i = 0; i < ds.getRowCount(); i++) {
      assertTrue(ds.getString(i, 0).contains("EPSG:27572"));
    }
    ds.close();
  }
Example #25
0
  private void testMetadataEditionListenerTest(String dsName, Type type) throws Exception {
    DataSource d = dsf.getDataSource(dsName);

    d.open();
    ListenerCounter elc = new ListenerCounter();
    d.addMetadataEditionListener(elc);
    d.removeField(1);
    d.addField("nuevo", type);
    d.setFieldName(1, "jjjj");
    assertEquals(elc.fieldDeletions, 1);
    assertEquals(elc.fieldInsertions, 1);
    assertEquals(elc.fieldModifications, 1);
    assertEquals(elc.total, 3);
    d.close();
  }
  @Test
  public void testIndexVisitor() throws Exception {
    DiskRTree tree = new DiskRTree(16, 1024, false);
    tree.newIndex(indexFile);
    DataSource ds = dsf.getDataSource("points");
    String fieldName = "the_geom";

    ds.open();
    int fieldIndex = ds.getFieldIndexByName(fieldName);
    for (int i = 0; i < ds.getRowCount(); i++) {
      Envelope value = ds.getFieldValue(i, fieldIndex).getAsGeometry().getEnvelopeInternal();
      tree.insert(value, i);
    }
    Envelope e = ds.getGeometry((ds.getRowCount() - 1) / 2).getEnvelopeInternal();

    IV iV = new IV(ds);
    tree.query(e, iV);

    assertTrue(iV.fired);
  }
  @Test
  public void testST_Transform() throws Exception {
    dsf.executeSQL(
        "CREATE TABLE init AS SELECT 'POINT(584173.736059813 2594514.82833411)'::GEOMETRY as the_geom;");
    DataSource ds = dsf.getDataSourceFromSQL("SELECT * from init;");
    // EPSG:27572;584173.736059813;2594514.82833411;EPSG:4326;;0.01

    WKTReader wKTReader = new WKTReader();
    Geometry targetGeom = wKTReader.read("POINT(2.114551393 50.345609791)");
    ds.open();
    assertTrue(!ds.isNull(0, 0));
    ds.close();

    ds =
        dsf.getDataSourceFromSQL(
            "SELECT ST_TRANSFORM(the_geom, 'EPSG:27572', 'EPSG:4326') from init;");

    ds.open();

    assertTrue(ds.getGeometry(0).equalsExact(targetGeom, 0.01));

    ds.close();
  }
 public void testAlphanumericObjectfailedCommit() throws Exception {
   DataSource ds = dsf.getDataSource("object");
   ds.open();
   failedCommit(ds, new FooQuery());
 }
 private void failedCommit(DataSource ds, IndexQuery query)
     throws DriverException, NonEditableDataSourceException {
   ds.deleteRow(2);
   ds.setFieldValue(0, 1, ValueFactory.createValue("nouveau"));
   Value[] row = ds.getRow(0);
   row[1] = ValueFactory.createValue("aaaaa");
   ds.insertFilledRow(row);
   Value[][] table = super.getDataSourceContents(ds);
   Iterator<Integer> it = ds.queryIndex(query);
   try {
     ReadDriver.failOnWrite = true;
     ds.commit();
     ds.close();
   } catch (DriverException e) {
     assertTrue(equals(table, super.getDataSourceContents(ds)));
     if (it != null) {
       assertTrue(ds.queryIndex(query) != null);
     } else {
       assertTrue(ds.queryIndex(query) == null);
     }
     ReadDriver.failOnWrite = false;
     ds.commit();
     ds.close();
   }
   ds.open();
   assertTrue(equals(table, super.getDataSourceContents(ds)));
   ds.close();
 }
 public void testAlphanumericDBFailOnWrite() throws Exception {
   DataSource ds = dsf.getDataSource("executeDB");
   ds.open();
   ReadDriver.setCurrentDataSource(ds);
   failedCommit(ds, new FooQuery());
 }