Development ha scritto:
Hi ,

when i change in my pomfile the geotools version from 2.6.0 to 2.6.1 i get these errors during runtime

Hi,
just took your sample application for a spin to check both errors and
performance.

I don't have your data set, but I've downloaded two data sets with a dozen shapefiles each, one small, one medium. Both you can download
yourself if you want, they are freely downloadable here:
http://www2.census.gov/cgi-bin/shapefiles2009/national-files

The small one is the shapefile set from Curch Falls county, Virginia,
it's 10 shapefiles amounting to 1.1MB.
The medium one is Gallatin county, Montana, it's 11 shapefiles
amounting to 129MB

I've modified your sample to load all the shapefiles in a certain
directory, added optimized data loading hint, removed translucent
fills which are always damn heavy to paint, and added a listener
that reports how much time it takes to render a certain scene.

For the small set the rendering time never went below 200ms, and
then I've found that the JMapPane subsystem polls the background
renderer only every 200ms. Modified the GeoTools sources to make it
pool every 20ms to get a more responsive GUI (Michael, the default
should really be lower than 200ms and user modifiable as well, or
else have a way for the executor to notify the rendering is done
without having to wait for the next poll).

Anyways, for the small shapefile set I have rendering times around
40ms, for the big one around 0.7s.
Neither of them seem slow to me, compared to the amount of data
being rendered.

Of course a lot depends on a number of details:
- enabling antialiasing doubles the rendering time
- using translucency has the same effect
- on my OS (Linux) the VM defaults to server mode, which makes it
  start slower but perform better in the long run
- on Linux loading shapefiles is faster, that cannot be helped,
  the file system portion of the OS is just superior
- the choice of JDK is important, using a Sun JDK 1.6 is crucial

From there to play anymation you'll have to make some custom
extensions I think, the portion of the rendering that makes
the background should be saved in a buffered image to avoid
re-rendering it, and just the moving cars should be re-rendered
over time. The full rendering should be done only when resizing
the window, changing base layers visibily and so on.

Hope this helps

Cheers
Andrea

PS: the code should be improved to move the polygon layers
to the bottom, but it was not the focus of the exercise so
I did not do it


--
Andrea Aime
OpenGeo - http://opengeo.org
Expert service straight from the developers.
package org.geotools;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.geotools.data.DataStore;
import org.geotools.data.DefaultRepository;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.DefaultMapLayer;
import org.geotools.map.MapContext;
import org.geotools.map.MapLayer;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Fill;
import org.geotools.styling.Graphic;
import org.geotools.styling.LineSymbolizer;
import org.geotools.styling.Mark;
import org.geotools.styling.PointSymbolizer;
import org.geotools.styling.PolygonSymbolizer;
import org.geotools.styling.Rule;
import org.geotools.styling.Stroke;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.JMapPane;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.swing.event.MapPaneAdapter;
import org.geotools.swing.event.MapPaneEvent;
import org.geotools.swing.event.MapPaneListener;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;

public class MultiShape extends JMapFrame {

    private MapContext context;

    private String title;

    private DefaultRepository repository = new DefaultRepository();

    static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);

    static FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);

    public void loadShapefilesfromDirectory(String dirPath) throws IOException {
        File directory = new File(dirPath);
        if (!directory.exists() || !directory.isDirectory())
            throw new IllegalArgumentException("Invalid path " + dirPath);

        File[] shapes = directory.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".shp");
            }
        });
        for (File f : shapes) {
            addShapefile(f.toURL(), true);
        }

        // expand area of interest
        try {
            context.setAreaOfInterest(context.getLayerBounds());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loadShapefile() throws IOException {

        File file = JFileDataStoreChooser.showOpenFile("shp", this);
        if (file != null) {
            addShapefile(file.toURL(), true);

        }
    }

    public MultiShape(String frameTitle) throws IOException {
        setTitle(frameTitle);
        enableLayerTable(true);
        enableStatusBar(true);
        enableToolBar(true);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu menu = new JMenu("File");
        JMenuItem item = new JMenuItem("Open...");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    loadShapefile();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        menu.add(item);

        item = new JMenuItem("Get scale");
        item.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JMapPane mapPane = getMapPane();
                if (mapPane != null) {
                    AffineTransform tr = mapPane.getScreenToWorldTransform();
                    if (tr != null) {
                        System.out.println("x scale: " + tr.getScaleX());
                        System.out.println("Y scale: " + tr.getScaleY());
                        System.out.println("pane area: " + mapPane.getVisibleRect());
                    }
                }
            }
        });
        menu.add(item);

        menuBar.add(menu);

        initComponents();

        // loadShapefilesfromDirectory("/home/aaime/devel/gisData/tiger2009/virginia_fallschurch");
        loadShapefilesfromDirectory("/home/aaime/devel/gisData/tiger2009/montana_gallatin");
        enableLayerTable(false);
        getMapPane().addMapPaneListener(new MapPaneAdapter() {
            long start;

            @Override
            public void onRenderingStarted(MapPaneEvent ev) {
                start = System.currentTimeMillis();
            }

            @Override
            public void onRenderingStopped(MapPaneEvent ev) {
                System.out.println("Map rendered in " + (System.currentTimeMillis() - start)
                        / 1000.0 + "s");
            }
        });
    }

    public static void main(String[] args) throws IOException {

        // Creating a new JMapFrame
        final MultiShape viewer = new MultiShape("Shapefile viewer");
        viewer.setSize(800, 800);
        viewer.setVisible(true);

    }

    public boolean addShapefile(URL shapefileURL, boolean defaultStyle) throws IOException {
        if (shapefileURL == null) {
            throw new IllegalArgumentException("shapefileURL must not be null");
        }
        ShapefileDataStore dstore = null;

        DataStore found = repository.dataStore(shapefileURL.toString());
        if (found != null && found instanceof ShapefileDataStore) {
            dstore = (ShapefileDataStore) found;
        } else {
            try {
                dstore = new ShapefileDataStore(shapefileURL);
            } catch (MalformedURLException urlEx) {
                throw new RuntimeException(urlEx);
            }
            repository.register(shapefileURL.toString(), dstore);
        }

        /*
         * We assume from this point that the shapefile exists and is accessible
         */
        String typeName = dstore.getTypeNames()[0];

        // Create a basic Style to render the features
        Style style = null;

        SimpleFeatureType schema = dstore.getSchema();
        Class geomType = schema.getGeometryDescriptor().getType().getBinding();

        if (Polygon.class.isAssignableFrom(geomType)
                || MultiPolygon.class.isAssignableFrom(geomType)) {
            style = createPolygonStyle();

        } else if (LineString.class.isAssignableFrom(geomType)
                || MultiLineString.class.isAssignableFrom(geomType)) {
            style = createLineStyle();

        } else {
            style = createPointStyle();
        }

        MapLayer layer = new DefaultMapLayer(dstore.getFeatureSource(typeName), style);
        addLayer(layer);

        return true;
    }

    /**
     * Add a map layer to those displayed. If no {...@linkplain org.geotools.map.MapContext} has been
     * set explicitly, a new instance of {...@linkplain org.geotools.map.DefaultMapContext} will be
     * created.
     */
    public void addLayer(MapLayer layer) {
        if (context == null) {
            CoordinateReferenceSystem crs = layer.getBounds().getCoordinateReferenceSystem();
            if (crs == null) {
                crs = DefaultGeographicCRS.WGS84;
            }
            context = new DefaultMapContext(crs);
            context.setTitle(title);
            setMapContext(context);

            StreamingRenderer renderer = new StreamingRenderer();
            HashMap hints = new HashMap();
            hints.put(StreamingRenderer.OPTIMIZED_DATA_LOADING_KEY, Boolean.TRUE);
            renderer.setRendererHints(hints);
            // renderer.setJava2DHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            // RenderingHints.VALUE_ANTIALIAS_ON));
            setRenderer(renderer);
        }

        context.addLayer(layer);
    }

    // Extension with Styles

    /**
     * Create a Style to draw polygon features with a thin blue outline and a cyan fill
     */
    private static Style createPolygonStyle() {

        // create a partially opaque outline stroke
        Stroke stroke = styleFactory.createStroke(filterFactory.literal(Color.BLUE), filterFactory
                .literal(1), null); /* filterFactory.literal(0.5)); */

        // create a partial opaque fill
        Fill fill = styleFactory.createFill(filterFactory.literal(Color.CYAN), null); 
                /* filterFactory.literal(0.5)); */

        /*
         * Setting the geometryPropertyName arg to null signals that we want to draw the default
         * geometry of features
         */
        PolygonSymbolizer sym = styleFactory.createPolygonSymbolizer(stroke, fill, null);

        Rule rule = styleFactory.createRule();
        rule.symbolizers().add(sym);
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[] { rule });
        Style style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);

        return style;
    }

    /**
     * Create a Style to draw line features as thin blue lines
     */
    private static Style createLineStyle() {
        Stroke stroke = styleFactory.createStroke(filterFactory.literal(Color.BLUE), filterFactory
                .literal(1));

        /*
         * Setting the geometryPropertyName arg to null signals that we want to draw the default
         * geomettry of features
         */
        LineSymbolizer sym = styleFactory.createLineSymbolizer(stroke, null);

        Rule rule = styleFactory.createRule();
        rule.symbolizers().add(sym);
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[] { rule });
        Style style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);

        return style;
    }

    /**
     * Create a Style to draw point features as circles with blue outlines and cyan fill
     */
    private static Style createPointStyle() {
        Graphic gr = styleFactory.createDefaultGraphic();

        Mark mark = styleFactory.getCircleMark();

        mark.setStroke(styleFactory.createStroke(filterFactory.literal(Color.BLUE), filterFactory
                .literal(1)));

        mark.setFill(styleFactory.createFill(filterFactory.literal(Color.CYAN)));

        gr.graphicalSymbols().clear();
        gr.graphicalSymbols().add(mark);
        gr.setSize(filterFactory.literal(5));

        /*
         * Setting the geometryPropertyName arg to null signals that we want to draw the default
         * geomettry of features
         */
        PointSymbolizer sym = styleFactory.createPointSymbolizer(gr, null);

        Rule rule = styleFactory.createRule();
        rule.symbolizers().add(sym);
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[] { rule });
        Style style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);

        return style;
    }

}
------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to