阅读:3032回复:2
如何编程实现在Geotools的JMapPane中显示MIF数据
如何编程实现在Geotools的JMapPane中显示MapInfo MIF格式的数据? 我是新手,麻烦大侠们说详细些。首先谢谢各位了!!
|
|
|
1楼#
发布于:2007-08-13 18:53
<P>经过努力 终于可以读取并显示MapInfo MIF地图数据了,但是每次运行后点一下工具栏上的Reset(地球图标)工具才能将地图数据全部显示。问题出在mapArea上,不知道怎么才能得到合适的mapArea,如果哪位弄好了也发一下。</P>
<P>import java.awt.BorderLayout;<BR>import java.awt.Container;<BR>import java.util.HashMap;</P> <P>import javax.swing.Action;<BR>import javax.swing.JFrame;<BR>import javax.swing.JToolBar;</P> <P>import org.geotools.data.FeatureReader;<BR>import org.geotools.data.mif.MIFDataStoreFactory;<BR>import org.geotools.data.mif.MIFFile;<BR>import org.geotools.feature.Feature;<BR>import org.geotools.feature.FeatureCollection;<BR>import org.geotools.feature.FeatureCollections;<BR>import org.geotools.feature.FeatureType;<BR>import org.geotools.gui.swing.JMapPane;<BR>import org.geotools.gui.swing.PanAction;<BR>import org.geotools.gui.swing.ResetAction;<BR>import org.geotools.gui.swing.SelectAction;<BR>import org.geotools.gui.swing.ZoomInAction;<BR>import org.geotools.gui.swing.ZoomOutAction;<BR>import org.geotools.map.DefaultMapContext;<BR>import org.geotools.map.MapContext;<BR>import org.geotools.referencing.crs.DefaultGeographicCRS;<BR>import org.geotools.renderer.GTRenderer;<BR>import org.geotools.renderer.lite.StreamingRenderer;<BR>import org.geotools.styling.BasicLineStyle;<BR>import org.geotools.styling.Style;</P> <P>import com.vividsolutions.jts.geom.Envelope;</P> <P><BR>public class JMapPaneDemo extends JFrame {<BR> <BR> private static final long serialVersionUID = 1L;<BR> <BR> private JToolBar toolBar = new JToolBar();<BR> private JMapPane mapPane = new JMapPane();<BR> <BR> public JMapPaneDemo() {<BR> Container contentPane = getContentPane();<BR> contentPane.setLayout(new BorderLayout());<BR> <BR> contentPane.add(toolBar, BorderLayout.NORTH); <BR> contentPane.add(mapPane, BorderLayout.CENTER);<BR> <BR> Action zoomIn = new ZoomInAction(mapPane);<BR> Action zoomOut = new ZoomOutAction(mapPane);<BR> Action pan = new PanAction(mapPane);<BR> Action select = new SelectAction(mapPane);<BR> Action reset = new ResetAction(mapPane);<BR> toolBar.add(zoomIn);<BR> toolBar.add(zoomOut);<BR> toolBar.add(pan);<BR> toolBar.add(select);<BR> toolBar.add(reset);<BR> <BR> setBounds(20, 20, 600, 400);<BR> setVisible(true);<BR> }<BR> <BR> public void load(String mifFilePath)throws Exception {<BR> MIFDataStoreFactory factory = new MIFDataStoreFactory();<BR> HashMap params = new HashMap();<BR> params.put(MIFDataStoreFactory.PARAM_FIELDCASE, "upper");<BR> params.put(MIFDataStoreFactory.PARAM_GEOMNAME, "GEOM");<BR> params.put(MIFDataStoreFactory.PARAM_GEOMTYPE, "typed");<BR> MIFFile mf = new MIFFile(mifFilePath, params);<BR> FeatureType ft = mf.getSchema();<BR> FeatureReader featureReader = mf.getFeatureReader(); <BR> <BR> Envelope mapArea = null;<BR> FeatureCollection collection = FeatureCollections.newCollection();<BR> while (featureReader.hasNext()) {<BR> Feature feature = featureReader.next();<BR> collection.add(feature);<BR> int count = 0;<BR> if (count == 0) mapArea = feature.getBounds();<BR> count++;<BR> }<BR> mapPane.setMapArea(mapArea);<BR> featureReader.close();<BR> <BR> MapContext mapContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);<BR> Style style = new BasicLineStyle();<BR> mapContext.addLayer(collection, style);<BR> mapContext.getLayerBounds();<BR> mapPane.setHighlightLayer(mapContext.getLayer(0));<BR> <BR> GTRenderer gtRenderer;<BR> gtRenderer = new StreamingRenderer();<BR> mapPane.setRenderer(gtRenderer);<BR> mapPane.setContext(mapContext);<BR> <BR> repaint();<BR> }<BR> <BR> public static void main(String[] args) {<BR> JMapPaneDemo application = new JMapPaneDemo();<BR> application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<BR> <BR> String mifFilePath = "yourfilename.mif";<BR> try {<BR> application.load(mifFilePath);<BR> } catch (Exception e) {<BR> e.printStackTrace();<BR> }<BR> }<BR>}<BR></P> |
|
|
2楼#
发布于:2007-08-17 20:10
你可以干脆设置一个固定大小,将所有的图都转换成这个大小
|
|
|