Wechat: yu389741| Email: gisdqy@163.com

Shop:https://www.giserdqy.com/shop

GEoTools学习笔记—显示三维效果–geotools-renderer3d


  1. /*
  2. *    GeoTools – The Open Source Java GIS Toolkit
  3. *    http://geotools.org
  4. *
  5. *    (C) 2007-2008, Open Source Geospatial Foundation (OSGeo)
  6. *
  7. *    This library is free software; you can redistribute it and/or
  8. *    modify it under the terms of the GNU Lesser General Public
  9. *    License as published by the Free Software Foundation;
  10. *    version 2.1 of the License.
  11. *
  12. *    This library is distributed in the hope that it will be useful,
  13. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. *    Lesser General Public License for more details.
  16. */ 
  17. package org.geotools.renderer3d.example; 
  18. import org.geotools.data.FeatureSource; 
  19. import org.geotools.data.shapefile.ShapefileDataStore; 
  20. import org.geotools.factory.CommonFactoryFinder; 
  21. import org.geotools.gui.swing.JMapPane; 
  22. import org.geotools.map.DefaultMapContext; 
  23. import org.geotools.map.MapContext; 
  24. import org.geotools.referencing.crs.DefaultGeographicCRS; 
  25. import org.geotools.renderer.lite.StreamingRenderer; 
  26. import org.geotools.renderer3d.Renderer3D; 
  27. import org.geotools.renderer3d.Renderer3DImpl; 
  28. import org.geotools.renderer3d.utils.canvas3d.FrameListener; 
  29. import org.geotools.styling.BasicLineStyle; 
  30. import org.geotools.styling.SLDParser; 
  31. import org.geotools.styling.Style; 
  32. import org.geotools.styling.StyleFactory; 
  33. import org.opengis.referencing.crs.CoordinateReferenceSystem; 
  34. import javax.swing.*; 
  35. import java.awt.BorderLayout; 
  36. import java.awt.Component; 
  37. import java.awt.Cursor; 
  38. import java.awt.Dimension; 
  39. import java.awt.event.ActionEvent; 
  40. import java.io.IOException; 
  41. import java.net.URL; 
  42. /**
  43. * An example of using the 3D map.
  44. *
  45. * @author Hans H鋑gstr鰉
  46. */ 
  47. public class Show3DMapExample 
  48.     //====================================================================== 
  49.     // Public Methods 
  50.     //———————————————————————- 
  51.     // Main Method 
  52.     public static
    void main( String[] args ) throws IOException 
  53.     { 
  54.         // Create some data 
  55. /* DEBUG:
  56.         final ExampleDataGenerator exampleDataGenerator = new ExampleDataGenerator();
  57.         final MapContext exampleMap = exampleDataGenerator.createExampleMap();
  58. */ 
  59.         final MapContext exampleMap = createContextFromShapefile(
    new URL( “file:example_data/countries/countries.shp” ), 
  60.                                                                  
    new URL( “file:example_data/simple_style.sld” ) ); 
  61.         // Create a 3D renderer 
  62.         final Renderer3D renderer3D =
    new Renderer3DImpl( exampleMap ); 
  63.         final Component mapView3D = renderer3D.get3DView(); 
  64.         // Create a 2D renderer with the same data for comparsion 
  65.         final StreamingRenderer streamingRenderer =
    new StreamingRenderer(); 
  66.         final JMapPane mapView2D =
    new JMapPane( streamingRenderer, exampleMap ); 
  67.         mapView2D.setMapArea( exampleMap.getLayerBounds() ); 
  68.         mapView2D.setState( JMapPane.Pan ); 
  69.         mapView2D.setCursor( new Cursor( Cursor.MOVE_CURSOR ) ); 
  70.         // Build and show the rest of the UI 
  71.         final JLabel labelFor3DView =
    new JLabel( “3D map renderer” ); 
  72.         createUi( wrapInTitledPanel( mapView3D, labelFor3DView ), 
  73.                   wrapInTitledPanel( mapView2D, new JLabel(
    “2D JMapPanel view” ) ) ); 
  74.         // Show frames per second in a swing label 
  75.         renderer3D.addFrameListener( createFpsDisplayer( labelFor3DView,
    “3D map renderer” ) ); 
  76.     } 
  77.     //====================================================================== 
  78.     // Private Methods 
  79.     private static FrameListener createFpsDisplayer(
    final JLabel labelFor3DView, final String message ) 
  80.     { 
  81.         return new FrameListener() 
  82.         { 
  83.             private double time_s =
    0
  84.             private
    int frames = 0
  85.             public void onFrame(
    final double secondsSinceLastFrame ) 
  86.             { 
  87.                 time_s += secondsSinceLastFrame; 
  88.                 frames++; 
  89.                 if ( time_s >
    1 ) // Calculate average fps over one second 
  90.                 { 
  91.                     final int fps = (int) ( frames / time_s ); 
  92.                     labelFor3DView.setText( message +
    ” (“
    + fps + ” FPS)” ); 
  93.                     time_s = 0
  94.                     frames = 0
  95.                 } 
  96.             } 
  97.         }; 
  98.     } 
  99.  
  100.     private static JPanel wrapInTitledPanel( Component component,
    final JLabel label ) 
  101.     { 
  102.         final JPanel panel =
    new JPanel( new BorderLayout() ); 
  103.         panel.add( component, BorderLayout.CENTER ); 
  104.         panel.add( label, BorderLayout.NORTH ); 
  105.         return panel; 
  106.     } 
  107.  
  108.     private static
    void createUi( final Component view3D,
    final JComponent view2D ) 
  109.     { 
  110.         final JPanel mainPanel =
    new JPanel( new BorderLayout() ); 
  111.         // Add a menu also to demonstrate that the 3D view runs inside swing / awt. 
  112.         // TODO: The menu must be converted to a heavyweight awt component to overdraw the 3D awt canvas… 
     
  113.         // That requires using an AWT Frame, and that makes it hard to add Swing components. 
  114.         mainPanel.add( createMenuBar(), BorderLayout.NORTH ); 
  115.         // Show that menu renders on top of swing component but not awt canvas 
  116.         mainPanel.add( new JLabel(
    ”         “ ), BorderLayout.WEST ); 
  117.         // TODO: Do this already at the Renderer3D level if not otherwise solvable. 
  118.         // Wrap the 3D canvas in another JPanel, as it seems to have redraw problems otherwise? 
  119.         final JPanel view3DHolder =
    new JPanel( new BorderLayout() ); 
  120.         view3DHolder.add( view3D, BorderLayout.CENTER ); 
  121.         // Put 2D and 3D view in a split pane. 
  122.         final JSplitPane splitPane =
    new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, view3DHolder, view2D ); 
  123.         splitPane.setDividerLocation( 500 ); 
  124.         splitPane.setOneTouchExpandable(
    true
    ); 
  125.         mainPanel.add( splitPane, BorderLayout.CENTER ); 
  126.         showInFrame( mainPanel, “3D Map Demo” ); 
  127.     } 
  128.  
  129.     private static
    void showInFrame( final Component view,
    final String frameTitle ) 
  130.     { 
  131.         final JFrame frame3D =
    new JFrame( frameTitle ); 
  132.         final JPanel container =
    new JPanel( new BorderLayout() ); 
  133.         container.setPreferredSize( new Dimension(
    800, 600 ) ); 
  134.         frame3D.getContentPane().add( container ); 
  135.         container.add( view, BorderLayout.CENTER ); 
  136.         frame3D.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
  137.         frame3D.pack(); 
  138.         frame3D.setVisible( true ); 
  139.     } 
  140.  
  141.     private static JMenuBar createMenuBar() 
  142.     { 
  143.         final JMenuBar menuBar =
    new JMenuBar(); 
  144.         final JMenu menu =
    new JMenu( “Demo” ); 
  145.         menuBar.add( menu ); 
  146.         menu.add( new AbstractAction(
    “Exit”
  147.         { 
  148.             public
    void actionPerformed( final ActionEvent e ) 
  149.             { 
  150.                 System.exit( 0 ); 
  151.             } 
  152.         } ); 
  153.         return menuBar; 
  154.     } 
  155.  
  156.     private static MapContext createContextFromShapefile(
    final URL shape ) 
  157.             throws IOException 
  158.     { 
  159.         return createContextFromShapefile( shape,
    null ); 
  160.     } 
  161.     private static MapContext createContextFromShapefile(
    final URL shape, final URL sld ) 
  162.             throws IOException 
  163.     { 
  164.         return loadShapefile( shape, loadStyle( sld ) ); 
  165.     } 
  166.     private static Style loadStyle(
    final URL sld ) 
  167.             throws IOException 
  168.     { 
  169.         final Style style; 
  170.         if ( sld !=
    null
  171.         { 
  172.             final StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(
    null ); 
  173.             final SLDParser stylereader =
    new SLDParser( styleFactory, sld ); 
  174.             final Style[] styles = stylereader.readXML(); 
  175.             style = styles[ 0 ]; 
  176.         } 
  177.         else 
  178.         { 
  179.             style = new BasicLineStyle(); 
  180.         } 
  181.         return style; 
  182.     } 
  183.     private static MapContext loadShapefile(
    final URL shape, final Style style ) 
  184.             throws IOException 
  185.     { 
  186.         final ShapefileDataStore shapefileDataStore =
    new ShapefileDataStore( shape ); 
  187.         final FeatureSource featureSource = shapefileDataStore.getFeatureSource(); 
  188.         CoordinateReferenceSystem crs = featureSource.getSchema().getDefaultGeometry().getCoordinateSystem(); 
  189.         if ( crs == null
  190.         { 
  191.             crs = DefaultGeographicCRS.WGS84; 
  192.         } 
  193.         final MapContext context =
    new DefaultMapContext( crs ); 
  194.         context.addLayer( featureSource, style ); 
  195.         context.getLayerBounds(); 
  196.         return context; 
  197.     } 

三维GIS运行效果:

3D

GEoTools学习笔记—读取GeoTiff文件格式

转载自:https://blog.csdn.net/sxausgyy/article/details/8110269