⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferfeatures.java

📁 GIS缓冲区分析:ArcObjects缓冲区分析开发实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return;
          }
        }catch(Exception e){}//it's OK. The file doesn't exist

        // Set up the Fields...

        IFields pFields = null;
        IFieldsEdit pFieldsEdit = null;
        pFields = new Fields();
        pFieldsEdit = (IFieldsEdit) pFields;

        IField pField = null;
        IFieldEdit pFieldEdit = null;

        //Make the shape field it will need a geometry definition, with a spatial reference
        pField = new Field();
        pFieldEdit = (IFieldEdit) pField;
        pFieldEdit.setName("Shape");
        pFieldEdit.setType(esriFieldType.esriFieldTypeGeometry);

        IGeometryDef pGeomDef = null;
        IGeometryDefEdit pGeomDefEdit = null;
        pGeomDef = new GeometryDef();
        pGeomDefEdit = (IGeometryDefEdit) pGeomDef;
        pGeomDefEdit.setGeometryType(esriGeometryType.esriGeometryPolygon);
        pGeomDefEdit.setSpatialReferenceByRef(new UnknownCoordinateSystem());

        pFieldEdit.setGeometryDefByRef(pGeomDef);
        pFieldsEdit.addField(pField);

        //Add another miscellaneous text field
        pField = new Field();
        pFieldEdit = (IFieldEdit) pField;
        pFieldEdit.setLength(30);
        pFieldEdit.setName("Scale");
        pFieldEdit.setType(esriFieldType.esriFieldTypeDouble);
        pFieldsEdit.addField(pField);

        //Create the shapefile (some parameters apply to geodatabase options and can be defaulted as Nothing)
        IFeatureClass pFeatClass = null;
        pFeatClass = pFWS.createFeatureClass(strFClassName, pFields, null, null,
                                             esriFeatureType.esriFTSimple,
                                             "Shape", "");


        // Now, add the buffer polygon as a feature
        IFeatureCursor pFC = pFeatClass.IFeatureClass_insert(true);
        IFeatureBuffer pFB = pFeatClass.createFeatureBuffer();

        pFB.setShapeByRef(result);
        int index = pFB.getFields().findField("Scale");
        pFB.setValue(index,new Double(Double.parseDouble(_distance)));
        pFC.insertFeature(pFB);

      }
      catch (Exception e) {
        System.out.println("Error saving shapefile: " + e);

      }
    }

  }

  /**
	 * Initializes all the controls
   */
  public void display(){

    try {
      toolbarBean.setBuddyControl(mapBean);
      toc.setBuddyControl(mapBean);

      toolbarBean.addItem(new ControlsSelectFeaturesTool(),
                 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); //SelectFeatures
      toolbarBean.addItem(new ControlsMapZoomInTool(),
                             0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); //ZoomIn
      toolbarBean.addItem(new ControlsMapZoomOutTool(),
                             0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //ZoomOut
      toolbarBean.addItem(new ControlsMapPanTool(),
                             0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //Pan
      // ZoominFixed
      toolbarBean.addItem(new ControlsMapZoomInFixedCommand(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      // ZoomoutFixed
      toolbarBean.addItem(new ControlsMapZoomOutFixedCommand(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      //MapZoomPan
      toolbarBean.addItem(new ControlsMapZoomPanTool(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      // MapFullExtent
      toolbarBean.addItem(new ControlsMapFullExtentCommand(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      //ZoomToLastExtentBack
      toolbarBean.addItem(new ControlsMapZoomToLastExtentBackCommand(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      // ZoomToLastExtentForward
      toolbarBean.addItem(new ControlsMapZoomToLastExtentForwardCommand(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      // Select
      toolbarBean.addItem(new ControlsSelectTool(),
                 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);


    }catch (Exception ex) {
      ex.printStackTrace();
    }

  }

  /** 
	 * Diplays file chooser dialog to add shape file
   */
  private void addShapefile(){

    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new javax.swing.filechooser.FileFilter() {
      public boolean accept(java.io.File f) {
        if (f.isDirectory())
          return true;
        if (f.getName().endsWith(".shp") || f.getName().endsWith(".SHP"))
          return true; //INN
        return false;
      }

      // The description of this filter
      public String getDescription() {
        return "Shapefiles";
      }
    }

    );

    fc.showOpenDialog(this);

    if(fc.getSelectedFile() != null){
      try {
        IFeatureLayer f = new FeatureLayer();
        IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
        IWorkspace ws = factory.openFromFile(fc.getSelectedFile().getParent(),
                                             0);

        IFeatureWorkspace fws = new IFeatureWorkspaceProxy(ws);
        IFeatureClass fclass = fws.openFeatureClass(fc.getSelectedFile().
            getName());

        f.setFeatureClassByRef(fclass);
        f.setName(fc.getSelectedFile().getName());

        mapBean.getActiveView().getFocusMap().addLayer(f);
        mapBean.getActiveView().refresh();

      }
      catch (Exception ex) {
        System.out.println("Exception in addShapefile...");
        ex.printStackTrace();
      }
    }

  }

  public static void main(String[] args) {

    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      EngineInitializer.initializeVisualBeans();
      AoInitialize aoInit = new AoInitialize();
      aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

      BufferFeatures bufferFeatures = new BufferFeatures();
      bufferFeatures.setSize(780,500);
      bufferFeatures.setTitle("BufferFeatures - ArcObjects Java SDK Developer Sample");
      bufferFeatures.setVisible(true);
      bufferFeatures.display();
      bufferFeatures.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }

  /** 
	 * Dialog to display buffer attributes.
   */
  class BufferUI extends JDialog {
    JPanel panel1 = new JPanel();
    JSlider jSlider1 = new JSlider();
    JCheckBox jCheckBox1 = new JCheckBox();
    JTextField jTextField1 = new JTextField();
    JButton btnCancel = new JButton();
    JButton btnOK = new JButton();
    JLabel jLabel1 = new JLabel();
    private String _distance;
    private String _shapefilePath;
    private boolean _saveBuffer;

    public BufferUI(Frame frame, String title, boolean modal) {
      super(frame, title, modal);
      try {
        buildUI();
        pack();
      }
      catch(Exception ex) {
        ex.printStackTrace();
      }
    }

    public BufferUI() {
      this(null, "", false);
    }
    private void buildUI() throws Exception {
      panel1.setLayout(null);
      this.getContentPane().setLayout(null);
      panel1.setBounds(new Rectangle(-1, 10, 400, 291));
      jSlider1.setMaximum(10);
      jSlider1.setMinimum(0);
      java.util.Hashtable labelTable = jSlider1.createStandardLabels(10,0);
      jSlider1.setLabelTable(labelTable);
      jSlider1.setMinorTickSpacing(1);
      jSlider1.setPaintLabels(true);
      jSlider1.setPaintTicks(true);
      jSlider1.setMinimumSize(new Dimension(36, 32));
      jSlider1.setBounds(new Rectangle(21, 37, 244, 40));
      jCheckBox1.setToolTipText("");
      jCheckBox1.setText("Save buffer to shapefile");
      jCheckBox1.setBounds(new Rectangle(21, 101, 164, 23));
      jTextField1.setText("jTextField1");
      jTextField1.setBounds(new Rectangle(21, 134, 242, 25));
      jTextField1.setText(System.getProperty("java.io.tmpdir"));
      this.setModal(true);

      // Action for the Cancel button...
      btnCancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
          btnCancel_actionPerformed(evt);
        }
      });

      // Action for the OK button...
      btnOK.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
          btnOK_actionPerformed(evt);
        }
      });

      btnCancel.setBounds(new Rectangle(114, 186, 75, 26));
      btnCancel.setText("Cancel");
      btnOK.setBounds(new Rectangle(211, 187, 77, 25));
      btnOK.setText("OK");
      jLabel1.setText("Set buffer distance");
      jLabel1.setBounds(new Rectangle(21, 17, 133, 15));
      panel1.add(jSlider1, null);
      panel1.add(jLabel1, null);
      panel1.add(jCheckBox1, null);
      panel1.add(jTextField1, null);
      panel1.add(btnOK, null);
      panel1.add(btnCancel, null);
      this.getContentPane().add(panel1, null);
    }

    public String getDistance(){
      return new String(jSlider1.getValue()+".0");
    }

    public boolean saveBuffer(){
      return jCheckBox1.isSelected();
    }

    void btnCancel_actionPerformed(ActionEvent e) {
      this.dispose();
    }

    void btnOK_actionPerformed(ActionEvent e) {
      doBuffer(getDistance());

      if(saveBuffer()){
        doSaveBuffer(jTextField1.getText());
      }
      this.dispose();
    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -