📄 esrishapeexport.java
字号:
/** * Prepares and returns a 7 column DbfTableModel to accept input * for columns of TYPE_CHARACTER. <br> * <br> * The default model used holds most of the DrawingAttributes of * the OMGraphics. * * * @param list the EsriGraphicList to create a DbfTableModel from. * @return The completed DbfTableModel. */ public static DbfTableModel createDefaultModel(EsriGraphicList list) { if (Debug.debugging("shape")) Debug.output("ESE: creating DbfTableModel"); DbfTableModel _model = new DbfTableModel(7); // Setup table structure // column 0 // The first parameter, 0, respresents the first column _model.setLength(0, (byte) 50); _model.setColumnName(0, SHAPE_DBF_DESCRIPTION); _model.setType(0, (byte) DbfTableModel.TYPE_CHARACTER); _model.setDecimalCount(0, (byte) 0); // column 1 // The first parameter, 1, respresents the second column _model.setLength(1, (byte) 10); _model.setColumnName(1, SHAPE_DBF_LINECOLOR); _model.setType(1, (byte) DbfTableModel.TYPE_CHARACTER); _model.setDecimalCount(1, (byte) 0); // column2 // The first parameter, 2, respresents the third column _model.setLength(2, (byte) 10); _model.setColumnName(2, SHAPE_DBF_FILLCOLOR); _model.setType(2, (byte) DbfTableModel.TYPE_CHARACTER); _model.setDecimalCount(2, (byte) 0); // column3 // The first parameter, 3, respresents the fourth column _model.setLength(3, (byte) 10); _model.setColumnName(3, SHAPE_DBF_SELECTCOLOR); _model.setType(3, (byte) DbfTableModel.TYPE_CHARACTER); _model.setDecimalCount(3, (byte) 0); // column4 // The first parameter, 4, respresents the fifth column _model.setLength(4, (byte) 4); _model.setColumnName(4, SHAPE_DBF_LINEWIDTH); _model.setType(4, (byte) DbfTableModel.TYPE_NUMERIC); _model.setDecimalCount(4, (byte) 0); // column5 // The first parameter, 5, respresents the sixth column _model.setLength(5, (byte) 20); _model.setColumnName(5, SHAPE_DBF_DASHPATTERN); _model.setType(5, (byte) DbfTableModel.TYPE_CHARACTER); _model.setDecimalCount(5, (byte) 0); // column6 // The first parameter, 6, respresents the seventh column _model.setLength(6, (byte) 10); _model.setColumnName(6, SHAPE_DBF_DASHPHASE); _model.setType(6, (byte) DbfTableModel.TYPE_NUMERIC); _model.setDecimalCount(6, (byte) 4); // At a later time, more stroke parameters can be addded, like // dash phase, end cap, line joins, and dash pattern. Iterator iterator = list.iterator(); while (iterator.hasNext()) { OMGraphic omg = (OMGraphic) iterator.next(); ArrayList record = new ArrayList(); // Description Object obj = omg.getAppObject(); if (obj instanceof String) { record.add(obj); } else { record.add(""); } record.add(ColorFactory.getHexColorString(omg.getLineColor())); record.add(ColorFactory.getHexColorString(omg.getFillColor())); record.add(ColorFactory.getHexColorString(omg.getSelectColor())); BasicStroke bs = (BasicStroke) omg.getStroke(); record.add(new Double(bs.getLineWidth())); String dp = BasicStrokeEditor.dashArrayToString(bs.getDashArray()); if (dp == BasicStrokeEditor.NONE) { dp = ""; } record.add(dp); record.add(new Double(bs.getDashPhase())); _model.addRecord(record); if (Debug.debugging("shape")) Debug.output("ESE: adding record: " + record); } return _model; } /** * Takes an OMPoly as the parameter and checks whether or not it * is a polygon or polyline. <br> * <br> * This method incorporates the OMPoly.isPolygon() method which * returns true if the fill color is not clear, but also checks * the first set and last set of lat/lon points of the float[] * defined by OMPoly.getLatLonArray(). Returns true for a polygon * and false for a polyline. * * @param omPoly the OMPoly object to be verified * @return The polygon value */ public static boolean isPolygon(OMPoly omPoly) { boolean isPolygon = false; // get the array of lat/lon points float[] points = omPoly.getLatLonArray(); int i = points.length; // compare the first and last set of points, equal points // verifies a polygon. if (points[0] == points[i - 2] && points[1] == points[i - 1]) { isPolygon = true; } // check OMPoly's definition of a polygon if (omPoly.isPolygon()) { isPolygon = true; } return isPolygon; } /** * Generic error handling, puts up an error window. */ protected void handleException(Exception e) { // System.out.println(e); StringBuffer sb = new StringBuffer("ShapeFile Export Error:"); sb.append("\nProblem with creating the shapefile set."); sb.append("\n" + e.toString()); JOptionPane.showMessageDialog(null, sb.toString(), "ESRI Shape Export to File", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } /** * Fetches a file path from the user, via a JFileChooser. Returns * null if the user cancels. * * @see com.bbn.openmap.util.FileUtils */ public String getFilePathFromUser() { return FileUtils.getFilePathToSaveFromUser("Select Name for Shape File Set..."); } /** * The main function is a test, reads in a Shape file (with the * .shx and .dbf files) and writes them back out. */ public static void main(String[] argv) { Debug.init(); ArgParser ap = new ArgParser("EsriShapeExport"); ap.add("shp", "A URL to a shape file (.shp).", 1); if (argv.length < 1) { ap.bail("", true); } ap.parse(argv); String[] files = ap.getArgValues("shp"); if (files != null && files[0] != null) { String shp = files[0]; String shx = null; String dbf = null; try { shx = shp.substring(0, shp.lastIndexOf('.') + 1) + PARAM_SHX; dbf = shp.substring(0, shp.lastIndexOf('.') + 1) + PARAM_DBF; DbfTableModel model = DbfTableModel.getDbfTableModel(PropUtils.getResourceOrFileOrURL(null, dbf)); EsriGraphicList list = EsriGraphicList.getEsriGraphicList(PropUtils.getResourceOrFileOrURL(null, shp), PropUtils.getResourceOrFileOrURL(null, shx), null, null); Debug.output(list.getDescription()); EsriShapeExport ese = new EsriShapeExport(list, model, null); ese.export(); } catch (MalformedURLException murle) { Debug.error("EsriShapeExport: Malformed URL Exception\n" + murle.getMessage()); } catch (NullPointerException npe) { Debug.error("EsriShapeExport: Path to shape file isn't good enough to find .dbf file and .shx file."); } catch (Exception exception) { Debug.error("EsriShapeExport: Exception\n" + exception.getMessage()); exception.printStackTrace(); } } else { ap.bail("Need a path to a Shape file (.shp)", true); } System.exit(0); } /** * A helper class to manage a specific instance of a * EsriGraphicList, it's data model, etc. Provides a GUI to * display and change the name of the file, and the DbfTableModel * GUI, and also writes the files out. */ public class ESEInterface { protected EsriGraphicList list; protected DbfTableModel model; protected String suffix; protected String filePath; File shpFile = null; File shxFile = null; File dbfFile = null; protected JTextField filePathField; public ESEInterface(EsriGraphicList eglist, String filePathString, String fileNameSuffix) { list = eglist; filePath = filePathString; model = eglist.getTable(); if (model == null) { model = createDefaultModel(list); } model.setWritable(true); suffix = (fileNameSuffix == null ? "" : fileNameSuffix); } public Component getGUI() { JPanel panel = new JPanel(); int type = list.getType(); String sectionTitle; switch (type) { case (SHAPE_TYPE_POINT): sectionTitle = "Point Shape File"; break; case (SHAPE_TYPE_POLYLINE): sectionTitle = "Line Shape File"; break; case (SHAPE_TYPE_POLYGON): sectionTitle = "Polygon Shape File"; break; default: sectionTitle = "Shape File"; } panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), sectionTitle)); panel.setLayout(new GridLayout(0, 1)); JPanel pathPanel = new JPanel(); filePathField = new JTextField(20); filePathField.setText(filePath + suffix); JButton filePathChooserLauncher = new JButton("Change Path"); filePathChooserLauncher.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setFilePath(getFilePathFromUser()); } }); panel.add(filePathField); JButton editDBFButton = new JButton("Edit the Attribute File..."); editDBFButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { model.showGUI(getFilePath() + " Attributes", DbfTableModel.DONE_MASK | DbfTableModel.MODIFY_COLUMN_MASK); } }); pathPanel.add(editDBFButton); pathPanel.add(filePathChooserLauncher); panel.add(pathPanel); return panel; } protected void setFilePath(String path) { filePath = path; } public void write() { if (filePathField != null) { filePath = filePathField.getText(); } if (filePath == null) { filePath = getFilePathFromUser(); if (filePath == null) { return; } } shpFile = new File(filePath + ".shp"); shxFile = new File(filePath + ".shx"); dbfFile = new File(filePath + ".dbf"); try { // create an esriGraphicList and export it to the // shapefile set if (DEBUG) Debug.output("ESE writing: " + list.size() + " elements"); ShpOutputStream pos = new ShpOutputStream(new FileOutputStream(shpFile)); int[][] indexData = pos.writeGeometry(list); ShxOutputStream xos = new ShxOutputStream(new FileOutputStream(shxFile)); xos.writeIndex(indexData, list.getType()); if (getWriteDBF()) { DbfOutputStream dos = new DbfOutputStream(new FileOutputStream(dbfFile)); dos.writeModel(model); } } catch (Exception e) { handleException(e); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -