?? bufferfeatures.java
字號:
/*
Copyright 1995-2005 ESRI
All rights reserved under the copyright laws of the United States.
You may freely redistribute and use this sample code, with or without modification.
Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY
WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
For additional information contact: Environmental Systems Research Institute, Inc.
Attn: Contracts Dept.
380 New York Street
Redlands, California, U.S.A. 92373
Email: contracts@esri.com
*/
/*
* ArcGIS Engine Developer Sample
* Application Name: BufferFeatures.java
*/
package com.esri.arcgis.samples.beans.mapcontrol;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.UIManager;
import com.esri.arcgis.beans.TOC.TOCBean;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.ISelection;
import com.esri.arcgis.controlcommands.ControlsMapFullExtentCommand;
import com.esri.arcgis.controlcommands.ControlsMapPanTool;
import com.esri.arcgis.controlcommands.ControlsMapZoomInFixedCommand;
import com.esri.arcgis.controlcommands.ControlsMapZoomInTool;
import com.esri.arcgis.controlcommands.ControlsMapZoomOutFixedCommand;
import com.esri.arcgis.controlcommands.ControlsMapZoomOutTool;
import com.esri.arcgis.controlcommands.ControlsMapZoomPanTool;
import com.esri.arcgis.controlcommands.ControlsMapZoomToLastExtentBackCommand;
import com.esri.arcgis.controlcommands.ControlsMapZoomToLastExtentForwardCommand;
import com.esri.arcgis.controlcommands.ControlsSelectFeaturesTool;
import com.esri.arcgis.controlcommands.ControlsSelectTool;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.display.IRgbColor;
import com.esri.arcgis.display.IRgbColorProxy;
import com.esri.arcgis.display.IScreenDisplay;
import com.esri.arcgis.display.ISimpleFillSymbol;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.ISymbolProxy;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.esriSimpleFillStyle;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.IEnumFeature;
import com.esri.arcgis.geodatabase.IEnumFeatureProxy;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureBuffer;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFeatureWorkspaceProxy;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFieldEdit;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IFieldsEdit;
import com.esri.arcgis.geodatabase.IGeometryDef;
import com.esri.arcgis.geodatabase.IGeometryDefEdit;
import com.esri.arcgis.geodatabase.IQueryFilter;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.geodatabase.esriFeatureType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IPolygon;
import com.esri.arcgis.geometry.IPolygonProxy;
import com.esri.arcgis.geometry.ITopologicalOperator;
import com.esri.arcgis.geometry.ITopologicalOperatorProxy;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.systemUI.esriCommandStyles;
/**
* This sample demonstrates how to buffer selected features in a layer, and
* how to display the resulting buffer polygon on a Map. The buffer distance
* is hard-coded to "1.0", which assumes that the shapefile data for the layer
* is stored in decimal degrees, for simplicity.
*
*/
public class BufferFeatures extends JFrame {
MapBean mapBean = new MapBean();
ToolbarBean toolbarBean = new ToolbarBean();
TOCBean toc = new TOCBean();
JPanel toolBarPanel = new JPanel();
JButton button = new JButton("Add a shapefile...");
JButton btnBuffer = new JButton("Buffer selected features");
IGeometry result = null;
String _distance = null;
public BufferFeatures() {
buildFrame();
}
/**
* Builds 'this' frame as per the following diagram:
*
* /----------------------------------------------------------\
* | BorderLayout.NORTH |
* | Toolbar Control |
* |--------------|-------------------------------------------|
* | | |
* | | |
* | TocControl | MapBean |
* | BorderLayout| BorderLayout.CENTER |
* | WEST | |
* | | |
* | | |
* | | |
* |--------------|-------------------------------------------|
*/
private void buildFrame(){
getContentPane().setLayout(new java.awt.BorderLayout());
getContentPane().add(mapBean,java.awt.BorderLayout.CENTER);
getContentPane().add(toolBarPanel,java.awt.BorderLayout.NORTH);
getContentPane().add(toc,java.awt.BorderLayout.WEST);
toolBarPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
toolBarPanel.add(button);
toolBarPanel.add(btnBuffer);
toolbarBean.setSize(450,20);
toc.setSize(150,580);
toolBarPanel.add(toolbarBean);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
//Browse to a shapefile and add a FeatureLayer to the map
addShapefile();
}
});
btnBuffer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
processBufferButtonAction(evt);
}
});
}
/**
* Processes buffer when buffer button clicked.
*/
public void processBufferButtonAction(ActionEvent evt){
// Buffer the selected Features...
BufferUI ui = new BufferUI(this,"Buffer Properties",true);
ui.setSize(300,250);
ui.setResizable(false);
ui.setVisible(true);
}
public void doBuffer(String distance){
_distance = distance;
try {
IScreenDisplay pScreenDisplay = mapBean.getActiveView().getScreenDisplay();
ISimpleFillSymbol pSymbol = new SimpleFillSymbol();
IRgbColor c = new RgbColor();
IRgbColor color = new IRgbColorProxy(c);
color.setRed(255);
color.setGreen(0);
color.setBlue(0);
pSymbol.setColor(color);
pSymbol.setStyle(esriSimpleFillStyle.esriSFSDiagonalCross);
ISymbol s = new ISymbolProxy(pSymbol);
ICursor[] fc = {};
IQueryFilter qfilter = new QueryFilter();
qfilter.setWhereClause(" ");
ISelection selection = mapBean.getActiveView().getFocusMap().getFeatureSelection();
IEnumFeature ef = new IEnumFeatureProxy(selection);
IFeature feature = ef.next();
ITopologicalOperator u = null;
while(feature != null){
ITopologicalOperator p = new ITopologicalOperatorProxy(feature.
getShape());
IGeometry buffer = p.buffer(Double.parseDouble(distance)); // Assumes a Geographic projection, for simplicity.
IPolygon poly = new IPolygonProxy(buffer);
if(u == null){
u = new ITopologicalOperatorProxy(poly);
}
result = u.union(poly);
feature = ef.next();
}
if(result != null){
pScreenDisplay.startDrawing(0, Short.parseShort("0"));
pScreenDisplay.setSymbol(s);
pScreenDisplay.drawPolygon(result);
pScreenDisplay.finishDrawing();
}else{
JOptionPane.showMessageDialog(this,"Please select features first!");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Saves shape file
*/
public void doSaveBuffer(String output){
java.io.File pFile = new java.io.File(output);
if(pFile.isDirectory()){
JOptionPane.showMessageDialog(this,"The specified file is actually a directory. Please enter a shapefile name.");
return;
}
String strFClassName = pFile.getName();
if(result != null){
try {
IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
IWorkspace ws = factory.openFromFile(pFile.getParent(), 0);
IFeatureWorkspace pFWS = new IFeatureWorkspaceProxy(ws);
// Make sure the featureclass doesn't already exist...
try{
IFeatureClass pTest = pFWS.openFeatureClass(strFClassName + ".shp");
if (pTest != null) {
JOptionPane.showMessageDialog(this,
strFClassName + " already exists. Please specify a unique shapefile name");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -