Search in sources :

Example 11 with FeatureStore

use of org.geotoolkit.storage.feature.FeatureStore in project geotoolkit by Geomatys.

the class DefaultFolderFeatureStore method isWritable.

/**
 * {@inheritDoc}
 */
@Override
public boolean isWritable(final String typeName) throws DataStoreException {
    typeCheck(typeName);
    final FeatureStore store = stores.get(this, typeName);
    return store.isWritable(typeName);
}
Also used : FeatureStore(org.geotoolkit.storage.feature.FeatureStore) AbstractFeatureStore(org.geotoolkit.storage.feature.AbstractFeatureStore)

Example 12 with FeatureStore

use of org.geotoolkit.storage.feature.FeatureStore in project geotoolkit by Geomatys.

the class ShapefileProvider method createDataStore.

/**
 * {@inheritDoc }
 * @param uri
 */
@Override
public FeatureStore createDataStore(final URI uri) throws DataStoreException {
    FeatureStore result;
    final Map params = Collections.singletonMap(PATH.getName().toString(), uri);
    try {
        result = (FeatureStore) DataStores.open(this, params);
    } catch (DataStoreException e) {
        result = (FeatureStore) this.create(Parameters.toParameter(params, getOpenParameters()));
    }
    return result;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Map(java.util.Map) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) IndexedShapefileFeatureStore(org.geotoolkit.data.shapefile.indexed.IndexedShapefileFeatureStore)

Example 13 with FeatureStore

use of org.geotoolkit.storage.feature.FeatureStore in project geotoolkit by Geomatys.

the class ReportDemo method main.

public static void main(String[] args) throws Exception {
    Demos.init();
    final InputStream template = ReportDemo.class.getResourceAsStream("/data/report/complexReport.jrxml");
    final Entry<JasperReport, FeatureType> entry = JasperReportService.prepareTemplate(template);
    final JasperReport report = entry.getKey();
    final FeatureType type = entry.getValue();
    System.out.println(type);
    // source to make an atlas ----------------------------------------------------
    final FeatureStore store = (FeatureStore) DataStores.open((Map) Collections.singletonMap("path", ReportDemo.class.getResource("/data/world/Countries.shp").toURI()));
    final GenericName name = store.getNames().iterator().next();
    final FeatureCollection countries = store.createSession(true).getFeatureCollection(new Query(name));
    // Iterator over all the countries --------------------------------------------
    final FeatureIterator ite = countries.iterator();
    // We map the feature type to the report type ---------------------------------
    final GenericMappingFeatureIterator mapped = new GenericMappingFeatureIterator(ite, new FeatureMapper() {

        @Override
        public FeatureType getSourceType() {
            return countries.getType();
        }

        @Override
        public FeatureType getTargetType() {
            return type;
        }

        @Override
        public Feature transform(Feature feature) {
            final Feature modified = type.newInstance();
            // create the main map with a single feature ------------------
            final FeatureCollection col = FeatureStoreUtilities.collection(feature);
            final MapLayers context = MapBuilder.createContext();
            final MutableStyle style = RandomStyleBuilder.createRandomVectorStyle(col.getType());
            final MapLayer layer = MapBuilder.createLayer(col);
            layer.setStyle(style);
            context.getComponents().add(layer);
            try {
                // add a custom decoration on our map.
                final GridTemplate gridTemplate = new DefaultGridTemplate(CommonCRS.WGS84.normalizedGeographic(), new BasicStroke(1.5f), new Color(120, 120, 120, 200), new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3, new float[] { 5, 5 }, 0), new Color(120, 120, 120, 60), new Font("serial", Font.BOLD, 12), Color.GRAY, 0, Color.WHITE, new Font("serial", Font.ITALIC, 10), Color.GRAY, 0, Color.WHITE);
                final PortrayalExtension ext = new PortrayalExtension() {

                    @Override
                    public void completeCanvas(J2DCanvas canvas) throws PortrayalException {
                        canvas.getContainer().getRoot().getChildren().add(new GraphicGridJ2D(canvas, gridTemplate));
                    }
                };
                final CanvasDef canvasDef = new CanvasDef(new Dimension(1, 1), null);
                canvasDef.setBackground(Color.WHITE);
                canvasDef.setStretchImage(false);
                canvasDef.setEnvelope(Envelopes.transform(context.getEnvelope().get(), CRS.forCode("EPSG:3395")));
                final SceneDef sceneDef = new SceneDef(context, null, ext);
                final MapDef mapdef = new MapDef(canvasDef, sceneDef, null);
                modified.setPropertyValue("map3", mapdef);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            // casual attributs -------------------
            modified.setPropertyValue("CNTRY_NAME", feature.getProperty("CNTRY_NAME").getValue());
            modified.setPropertyValue("POP_CNTRY", feature.getProperty("POP_CNTRY").getValue());
            // chart -------------------------
            final DefaultPieDataset pds = new DefaultPieDataset();
            pds.setValue((Comparable) feature.getProperty("SOVEREIGN").getValue(), Math.random());
            pds.setValue((Comparable) feature.getProperty("ISO_3DIGIT").getValue(), Math.random());
            final JFreeChart chart = ChartFactory.createPieChart("Info", pds, true, true, Locale.FRENCH);
            modified.setPropertyValue("chart4", new ChartDef(chart));
            // legend --------------------------
            modified.setPropertyValue("legend5", new LegendDef());
            // scale bar -------------------
            modified.setPropertyValue("scalebar6", new ScaleBarDef());
            // north arow -------------------
            modified.setPropertyValue("northarrow7", new NorthArrowDef());
            // subtable --------------
            final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
            ftb.setName("subdata");
            ftb.addAttribute(Integer.class).setName("men");
            ftb.addAttribute(Integer.class).setName("women");
            ftb.addAttribute(String.class).setName("desc");
            final FeatureType subType = ftb.build();
            final FeatureCollection subcol = FeatureStoreUtilities.collection("sub", subType);
            try {
                FeatureWriter fw = subcol.getSession().getFeatureStore().getFeatureWriter(Query.filtered(subType.getName().toString(), Filter.exclude()));
                for (int i = 0, n = new Random().nextInt(20); i < n; i++) {
                    Feature f = fw.next();
                    f.setPropertyValue("men", new Random().nextInt());
                    f.setPropertyValue("women", new Random().nextInt());
                    f.setPropertyValue("desc", "some text from attribut");
                    fw.write();
                }
                fw.close();
            } catch (DataStoreException ex) {
                ex.printStackTrace();
            }
            modified.setPropertyValue("table8", new CollectionDataSource(subcol));
            return modified;
        }
    });
    // Generate the report --------------------------------------------------------
    final OutputDef output = new OutputDef(JasperReportService.MIME_PDF, new File("atlas.pdf"));
    JasperReportService.generateReport(report, mapped, null, output);
}
Also used : BasicStroke(java.awt.BasicStroke) FeatureType(org.opengis.feature.FeatureType) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) ScaleBarDef(org.geotoolkit.report.graphic.scalebar.ScaleBarDef) MapLayer(org.apache.sis.portrayal.MapLayer) LegendDef(org.geotoolkit.report.graphic.legend.LegendDef) CollectionDataSource(org.geotoolkit.report.CollectionDataSource) JasperReport(net.sf.jasperreports.engine.JasperReport) DefaultGridTemplate(org.geotoolkit.display2d.ext.grid.DefaultGridTemplate) GridTemplate(org.geotoolkit.display2d.ext.grid.GridTemplate) Feature(org.opengis.feature.Feature) Font(java.awt.Font) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) GenericMappingFeatureIterator(org.geotoolkit.storage.memory.GenericMappingFeatureIterator) GenericName(org.opengis.util.GenericName) MutableStyle(org.geotoolkit.style.MutableStyle) Random(java.util.Random) SceneDef(org.geotoolkit.display2d.service.SceneDef) MapDef(org.geotoolkit.report.graphic.map.MapDef) PortrayalExtension(org.geotoolkit.display2d.service.PortrayalExtension) FeatureMapper(org.geotoolkit.storage.memory.mapping.FeatureMapper) J2DCanvas(org.geotoolkit.display2d.canvas.J2DCanvas) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) ChartDef(org.geotoolkit.report.graphic.chart.ChartDef) GraphicGridJ2D(org.geotoolkit.display2d.ext.grid.GraphicGridJ2D) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) DataStoreException(org.apache.sis.storage.DataStoreException) InputStream(java.io.InputStream) DefaultGridTemplate(org.geotoolkit.display2d.ext.grid.DefaultGridTemplate) Color(java.awt.Color) GenericMappingFeatureIterator(org.geotoolkit.storage.memory.GenericMappingFeatureIterator) Dimension(java.awt.Dimension) NorthArrowDef(org.geotoolkit.report.graphic.northarrow.NorthArrowDef) PortrayalException(org.geotoolkit.display.PortrayalException) DataStoreException(org.apache.sis.storage.DataStoreException) JFreeChart(org.jfree.chart.JFreeChart) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) OutputDef(org.geotoolkit.display2d.service.OutputDef) Map(java.util.Map) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) File(java.io.File) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 14 with FeatureStore

use of org.geotoolkit.storage.feature.FeatureStore in project geotoolkit by Geomatys.

the class ShapefileTest method testHolyPolygons.

@Test
public void testHolyPolygons() throws Exception {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(MultiPolygon.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    Collection<Feature> features = new ArrayList<>();
    File tmpFile = getTempFile();
    tmpFile.delete();
    // write features
    ShapefileProvider make = new ShapefileProvider();
    String pathId = ShapefileProvider.PATH.getName().getCode();
    FeatureStore s = (FeatureStore) make.create(Parameters.toParameter(Collections.singletonMap(pathId, tmpFile.toURI().toURL()), make.getOpenParameters()));
    s.createFeatureType(type);
    GenericName typeName = type.getName();
    Session session = s.createSession(true);
    session.addFeatures(typeName.toString(), features);
    session.commit();
    s = new ShapefileFeatureStore(tmpFile.toURI());
    typeName = s.getNames().iterator().next();
    FeatureCollection fc = s.createSession(true).getFeatureCollection(new Query(typeName));
    ShapefileReadWriteTest.compare(features, fc);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) GenericName(org.opengis.util.GenericName) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) File(java.io.File) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 15 with FeatureStore

use of org.geotoolkit.storage.feature.FeatureStore in project geotoolkit by Geomatys.

the class ShapefileQuadTreeReadWriteTest method testWriteTwice.

@Test
public void testWriteTwice() throws Exception {
    copyShapefiles("shapes/stream.shp");
    ShapefileProvider fac = new ShapefileProvider();
    FeatureStore s1 = createDataStore(fac, TestData.url(AbstractTestCaseSupport.class, "shapes/stream.shp"), true);
    GenericName typeName = s1.getNames().iterator().next();
    FeatureType type = s1.getFeatureType(typeName.toString());
    FeatureCollection one = s1.createSession(true).getFeatureCollection(new Query(typeName));
    ShapefileProvider maker = new ShapefileProvider();
    doubleWrite(type, one, getTempFile(), maker, false);
    doubleWrite(type, one, getTempFile(), maker, true);
}
Also used : FeatureType(org.opengis.feature.FeatureType) GenericName(org.opengis.util.GenericName) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) AbstractTestCaseSupport(org.geotoolkit.data.shapefile.AbstractTestCaseSupport) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) ShapefileProvider(org.geotoolkit.data.shapefile.ShapefileProvider) Test(org.junit.Test)

Aggregations

FeatureStore (org.geotoolkit.storage.feature.FeatureStore)28 GenericName (org.opengis.util.GenericName)11 AbstractFeatureStore (org.geotoolkit.storage.feature.AbstractFeatureStore)10 Query (org.geotoolkit.storage.feature.query.Query)9 DataStoreException (org.apache.sis.storage.DataStoreException)8 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)6 Test (org.junit.Test)6 FeatureType (org.opengis.feature.FeatureType)6 Session (org.geotoolkit.storage.feature.session.Session)5 Serializable (java.io.Serializable)4 HashMap (java.util.HashMap)4 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)3 Parameters (org.apache.sis.parameter.Parameters)3 ShapefileProvider (org.geotoolkit.data.shapefile.ShapefileProvider)3 Feature (org.opengis.feature.Feature)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MapLayer (org.apache.sis.portrayal.MapLayer)2 MapLayers (org.apache.sis.portrayal.MapLayers)2