/** * Create FeatureSchema using union of all keys from all selected primitives * * @param prims * @return */ private FeatureSchema createSchema(Collection<OsmPrimitive> prims) { Set<String> keys = new HashSet<>(); for (OsmPrimitive prim : prims) { keys.addAll(prim.getKeys().keySet()); } FeatureSchema schema = new FeatureSchema(); schema.addAttribute("__GEOMETRY__", AttributeType.GEOMETRY); for (String key : keys) { schema.addAttribute(key, AttributeType.STRING); } return schema; }
public static FeatureSchema createGeometryMsgFeatureSchema() { FeatureSchema featureSchema = new FeatureSchema(); featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY); featureSchema.addAttribute(MESG_ATTR_NAME, AttributeType.STRING); return featureSchema; }
public void addAttribute(String attributeName, AttributeType attributeType, String accessType) { super.addAttribute(attributeName, attributeType); int attributeIndex = getAttributeIndex(attributeName); attributeAccess.add(attributeIndex, accessType); }
// Sample to test the class public static void main(String[] args) { final LayerManager lm = new LayerManager(); // Schema containing a single Geometry attribute FeatureSchema fs1 = new FeatureSchema(); fs1.addAttribute("GEOMETRY", AttributeType.GEOMETRY); com.vividsolutions.jump.feature.FeatureDataset ds1 = new com.vividsolutions.jump.feature.FeatureDataset(fs1); lm.addLayer("", "LayerWithJustGeometry", ds1); // Schema containing a Geometry and a String attributes FeatureSchema fs2 = new FeatureSchema(); fs2.addAttribute("GEOMETRY", AttributeType.GEOMETRY); fs2.addAttribute("Name", AttributeType.STRING); com.vividsolutions.jump.feature.FeatureDataset ds2 = new com.vividsolutions.jump.feature.FeatureDataset(fs2); lm.addLayer("", "LayerWithStringAttribute", ds2); // Schema containing a Geometry, a String and a Integer attributes FeatureSchema fs3 = new FeatureSchema(); fs3.addAttribute("GEOMETRY", AttributeType.GEOMETRY); fs3.addAttribute("Name", AttributeType.STRING); fs3.addAttribute("Age", AttributeType.INTEGER); com.vividsolutions.jump.feature.FeatureDataset ds3 = new com.vividsolutions.jump.feature.FeatureDataset(fs3); lm.addLayer("", "LayerWithNumericAttribute", ds3); // MultiInputDialog usage demonstration final MultiInputDialog d = new MultiInputDialog(null, "Title!", true); d.setInset(2); d.addSubTitle("Subtitle 1"); d.addLabel("This is just a label"); d.addTextField("Name", "", 24, null, ""); d.addPositiveIntegerField("Age", 0, 6, ""); d.addNonNegativeDoubleField("Salary", 0, 12, ""); d.addComboBox( "Occupation", "Cadre", java.util.Arrays.asList("Manager", "Developper", "Technician", "Secretary"), ""); d.indentLabel("Occupation"); d.addSubTitle("Layer and attribute selection"); AttributeTypeFilter STRING_FILTER = new AttributeTypeFilter(AttributeTypeFilter.STRING); AttributeTypeFilter NUMERIC_FILTER = AttributeTypeFilter.NUMERIC_FILTER; AttributeTypeFilter NOGEOM_FILTER = AttributeTypeFilter.NO_GEOMETRY_FILTER; AttributeTypeFilter ALL_FILTER = AttributeTypeFilter.ALL_FILTER; final JComboBox typeChooser = d.addComboBox( "Choose Attribute Type", "ALL", Arrays.asList(STRING_FILTER, NUMERIC_FILTER, ALL_FILTER, NOGEOM_FILTER), ""); final JComboBox layerChooser = d.addLayerComboBox("LayerField", null, "ToolTip", lm); final JComboBox attributeChooser = d.addAttributeComboBox("Attribute field", "LayerField", NUMERIC_FILTER, ""); typeChooser.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { AttributeTypeFilter atf = (AttributeTypeFilter) typeChooser.getSelectedItem(); layerChooser.setModel(new DefaultComboBoxModel(atf.filter(lm).toArray(new Layer[0]))); } }); d.addSeparator(); final JCheckBox jcb = d.addCheckBox("Display icon", false, ""); JButton button = d.addButton("Switch image panel"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (d.infoPanel.getDescription().equals("")) { d.infoPanel.setDescription( "Description of the dialog box.\nThis description must be helpful for the user. I must give meaningful information about which parameters are mandatory, optional, what they represent and which value they can take."); d.getConsole().flashMessage("Add description"); } else { d.infoPanel.setDescription(""); d.getConsole().flashMessage("Remove description"); } d.pack(); } }); jcb.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (jcb.isSelected()) { d.infoPanel.setIcon( new ImageIcon( com.vividsolutions.jump.workbench.ui.images.IconLoader.class.getResource( "Butt.gif"))); d.getConsole().flashMessage("Add image"); } else { d.infoPanel.setIcon(null); d.getConsole().flashMessage("Remove image"); } d.pack(); } }); JButton button2 = d.addButton("Second button", "OK", ""); GUIUtil.centreOnScreen(d); d.setVisible(true); // System.out.println(d.getLayer("LayerField")); System.exit(0); }
private JButton getGraticuleButton() { jButtonGraticule = new JButton( I18N.get( MobileExtractPlugin.PluginMobileExtracti18n, MobilePluginI18NResource.MobileExtractPanel03_crearCuadricula)); LayerManager layerManager = context.getLayerManager(); Layer graticuleLayer = layerManager.getLayer(GraticuleCreatorEngine.getGraticuleName()); // si ya existe una capa cuadrícula significa que se trata de un fichero importado previamente a // ejecutar el asistente if (graticuleLayer != null) { jButtonGraticule.setEnabled(false); if (bCuadriculaCreada == true) { // solo ejecutamos este código una vez return jButtonGraticule; } FeatureCollectionWrapper graticuleCollectionWrapper = graticuleLayer.getFeatureCollectionWrapper(); FeatureSchema featureSchema = graticuleCollectionWrapper.getFeatureSchema(); // cuadriculas visionadas IViewport viewport = (IViewport) context.getLayerViewPanel().getViewport(); Envelope viewEnvelope = viewport.getEnvelopeInModelCoordinates(); List<Feature> graticuleFeatures = graticuleCollectionWrapper.query(viewEnvelope); // ordenamos por distancia al origen las features List<SortedFeature> sortedGraticuleFeatures = new ArrayList<SortedFeature>(); Feature feature = null; SortedFeature sortFeat = null; for (Iterator iterator = graticuleFeatures.iterator(); iterator.hasNext(); ) { feature = (Feature) iterator.next(); sortFeat = new SortedFeature(feature); sortedGraticuleFeatures.add(sortFeat); } Collections.sort(sortedGraticuleFeatures); // borramos el resto de cuadrículas graticuleCollectionWrapper.clear(); graticuleCollectionWrapper.addAll(graticuleFeatures); Envelope newWrapperEnvelope = graticuleCollectionWrapper.getEnvelope(); // zoom al envelope actual try { viewport.zoom(newWrapperEnvelope); } catch (NoninvertibleTransformException e) { log.warn("No se ha podido alcanzar el zoom " + newWrapperEnvelope); } // ajustamos la ventana al envelope de la cuadrícula resizeViewToEnvelope(context, newWrapperEnvelope, viewport); // guardamos las propiedades gráficas de la extracción Envelope featEnvelope = sortedGraticuleFeatures.get(0).getFeature().getGeometry().getEnvelopeInternal(); Double featWidth = featEnvelope.getWidth(); Double featHeight = featEnvelope.getHeight(); Double minX = newWrapperEnvelope.getMinX(); Double minY = newWrapperEnvelope.getMinY(); Coordinate coordCorner = new Coordinate(minX, minY); blackboard.put(GraticuleCreatorPlugIn.SOUTHWEST_CORNER_OF_LEFT_LAYER, coordCorner); blackboard.put(GraticuleCreatorPlugIn.CELL_SIDE_LENGTH_X, featWidth); blackboard.put(GraticuleCreatorPlugIn.CELL_SIDE_LENGTH_Y, featHeight); Integer numCeldasAncho = (int) (newWrapperEnvelope.getWidth() / featWidth); Integer numCeldasAlto = (int) (newWrapperEnvelope.getHeight() / featHeight); blackboard.put(GraticuleCreatorPlugIn.LAYER_WIDTH_IN_CELLS, numCeldasAncho); blackboard.put(GraticuleCreatorPlugIn.LAYER_HEIGHT_IN_CELLS, numCeldasAlto); // creamos celdas para los espacios en blanco if (sortedGraticuleFeatures.size() < numCeldasAncho * numCeldasAlto) { Double curMinX = minX; Double curMinY = minY; Double fMinX = 0.0; GeometryFactory geoFact = new GeometryFactory(); Feature newFeature = null; for (int i = 0; i < sortedGraticuleFeatures.size(); i++) { sortFeat = sortedGraticuleFeatures.get(i); feature = sortFeat.getFeature(); fMinX = feature.getGeometry().getEnvelopeInternal().getMinX(); if (curMinX.doubleValue() != fMinX.doubleValue()) { // creamos la feature newFeature = (Feature) feature.clone(); Coordinate[] coordArray = { new Coordinate(curMinX, curMinY), new Coordinate(curMinX + featWidth, curMinY), new Coordinate(curMinX + featWidth, curMinY + featHeight), new Coordinate(curMinX, curMinY + featHeight), new Coordinate(curMinX, curMinY) }; newFeature.setGeometry( geoFact.createPolygon(geoFact.createLinearRing(coordArray), null)); sortedGraticuleFeatures.add(i, new SortedFeature(newFeature)); graticuleCollectionWrapper.add(newFeature); } // para lo último else if ((i == sortedGraticuleFeatures.size() - 1) && ((minX + (featWidth * numCeldasAncho)) != (fMinX + featWidth))) { curMinX = fMinX + featWidth; newFeature = (Feature) feature.clone(); Coordinate[] coordArray = { new Coordinate(curMinX, curMinY), new Coordinate(curMinX + featWidth, curMinY), new Coordinate(curMinX + featWidth, curMinY + featHeight), new Coordinate(curMinX, curMinY + featHeight), new Coordinate(curMinX, curMinY) }; newFeature.setGeometry( geoFact.createPolygon(geoFact.createLinearRing(coordArray), null)); sortedGraticuleFeatures.add( sortedGraticuleFeatures.size(), new SortedFeature(newFeature)); graticuleCollectionWrapper.add(newFeature); continue; } if (((i + 1) % numCeldasAncho) != 0) { curMinX = curMinX + featWidth; } else { // ultima celda de cada fila curMinX = minX; curMinY += featHeight; } } } // añadimos el atributo cellid featureSchema.addAttribute(GraticuleCreatorEngine.ATR_CELL_ID, AttributeType.INTEGER); int k = 1; Object[] featAttribs = null; Object[] newFeatAttribs = null; for (Iterator iterator = sortedGraticuleFeatures.iterator(); iterator.hasNext(); k++) { feature = ((SortedFeature) iterator.next()).getFeature(); featAttribs = feature.getAttributes(); newFeatAttribs = new Object[featAttribs.length + 1]; for (int i = 0; i < featAttribs.length; i++) { newFeatAttribs[i] = featAttribs[i]; } newFeatAttribs[newFeatAttribs.length - 1] = k; feature.setAttributes(newFeatAttribs); } // pintamos el atributo en la capa LabelStyle labelStyle = graticuleLayer.getLabelStyle(); labelStyle.setColor(Color.RED); labelStyle.setHeight(labelStyle.getHeight() * 4); labelStyle.setAttribute(GraticuleCreatorEngine.ATR_CELL_ID); labelStyle.setEnabled(true); // evitamos que se modifiquen las cuadrículas graticuleLayer.setEditable(false); graticuleLayer.fireAppearanceChanged(); bCuadriculaCreada = true; } // llamada al graticulePlugin else { jButtonGraticule.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { GraticuleCreatorPlugIn graticulePlugin = new GraticuleCreatorPlugIn(); try { graticulePlugin.execute(context); } catch (Exception e1) { e1.printStackTrace(); } wizardContext.inputChanged(); // indicamos que ya se puede habilitar el boton } }); } return jButtonGraticule; }
private FeatureDataset classifyAndCreatePlot(TaskMonitor monitor, final PlugInContext context) throws Exception { monitor.report(this.sCalculateBreaks); // =============== get DATA and prepare ==============/ FeatureSchema fs = this.fc.getFeatureSchema(); AttributeType type = null; if ((fs.getAttributeType(this.selAttribute) == AttributeType.DOUBLE) || (fs.getAttributeType(this.selAttribute) == AttributeType.INTEGER)) { // -- move on type = fs.getAttributeType(this.selAttribute); } else { // System.out.println("ClassifyAttributesPlugIn: wrong datatype of chosen attribute"); context.getWorkbenchFrame().warnUser(sWrongDataType); return null; } int size = getFeatureCollectionSize(this.fc, this.selAttribute, this.nullAsZero); if (size < 3) { return null; } this.ranges = Math.min(this.ranges, size); double[] data = new double[size]; double[][] plotdata = new double[2][size]; // for drawing 1-D scatter plot int[] fID = new int[size]; int i = 0; for (Iterator iter = fc.iterator(); iter.hasNext(); ) { Feature f = (Feature) iter.next(); if (f.getAttribute(this.selAttribute) == null && !nullAsZero) continue; fID[i] = f.getID(); plotdata[1][i] = 1; Object val = f.getAttribute(this.selAttribute); if (type == AttributeType.DOUBLE) { if (val == null) data[i] = 0.0; else data[i] = ((Double) val).doubleValue(); } else if (type == AttributeType.INTEGER) { if (val == null) data[i] = 0; else data[i] = ((Integer) val).intValue(); } plotdata[0][i] = data[i]; i++; } /* //-- some testdata double[][] plotdata2 = new double[2][8]; double[] data2 = { -2, 4, 6, 5, 0, 10, 7, 1 }; double[] axis2 = { 1, 1, 1, 1, 1, 1, 1, 1 }; plotdata2[0] = data2; plotdata2[1] = axis2; */ if (monitor.isCancelRequested()) { return null; } // =============== find breaks according to chosen method ==============/ double[] limits = null; if (this.useKmeans == false) { if (this.selClassifier == Classifier1D.EQUAL_NUMBER) { limits = Classifier1D.classifyEqualNumber(data, this.ranges); } else if (this.selClassifier == Classifier1D.EQUAL_RANGE) { limits = Classifier1D.classifyEqualRange(data, this.ranges); } else if (this.selClassifier == Classifier1D.MEAN_STDEV) { limits = Classifier1D.classifyMeanStandardDeviation(data, this.ranges); } else if (this.selClassifier == Classifier1D.MAX_BREAKS) { limits = Classifier1D.classifyMaxBreaks(data, this.ranges); } else if (this.selClassifier == Classifier1D.JENKS_BREAKS) { limits = Classifier1D.classifyNaturalBreaks(data, this.ranges); } } else { if (this.selClassifier == Classifier1D.EQUAL_NUMBER) { limits = Classifier1D.classifyKMeansOnExistingBreaks(data, this.ranges, 3); } else if (this.selClassifier == Classifier1D.EQUAL_RANGE) { limits = Classifier1D.classifyKMeansOnExistingBreaks(data, this.ranges, 2); } else if (this.selClassifier == Classifier1D.MEAN_STDEV) { limits = Classifier1D.classifyKMeansOnExistingBreaks(data, this.ranges, 4); } else if (this.selClassifier == Classifier1D.MAX_BREAKS) { limits = Classifier1D.classifyKMeansOnExistingBreaks(data, this.ranges, 1); } else if (this.selClassifier == Classifier1D.JENKS_BREAKS) { limits = Classifier1D.classifyKMeansOnExistingBreaks(data, this.ranges, 5); } } if (monitor.isCancelRequested()) { return null; } monitor.report(this.sDisplayBreaks); // =============== plot data and class breaks ==============/ // -- do display here - in case we later want to allow interactive editing of the limits // -- reformat limits double[][] limits2show = new double[2][limits.length]; // -- due to bug in jmathplot add limits twice if only three classes = 2breaks are sought if (limits.length == 2) { limits2show = new double[2][limits.length * 2]; } for (int j = 0; j < limits.length; j++) { limits2show[0][j] = limits[j]; // x-axis limits2show[1][j] = Math.floor( i / (4.0 * this.ranges)); // y-axis, estimate height of "bar" from number of items // limits2show[1][j]= 1; // -- due to bug in jmathplot add limits twice if only three classes are sought if (limits.length == 2) { limits2show[0][limits.length + j] = limits[j]; limits2show[1][limits.length + j] = Math.floor(i / (4.0 * this.ranges)); } } // =============== plot data and class breaks ==============/ // -- create plots /*final Plot2DPanelOJ*/ plot = new Plot2DPanelOJ(); plot.addHistogramPlotOJ( this.selAttribute, data, this.ranges * 3, context, selLayer, this.selAttribute); plot.addScatterPlotOJ(this.sDatapoints, plotdata, fID, context, this.selLayer); plot.addBarPlot(this.sClassbreaks, limits2show); plot.plotToolBar.setVisible(true); plot.setAxisLabel(0, this.selAttribute); plot.setAxisLabel(1, this.sCount); plot.addLegend("SOUTH"); // [mmichaud 2012-04-09] Moved in run method after the addLayer method // to avoid the problem of the focus change // JInternalFrame frame = new JInternalFrame(this.sHistogram); // frame.setLayout(new BorderLayout()); // frame.add(plot, BorderLayout.CENTER); // frame.setClosable(true); // frame.setResizable(true); // frame.setMaximizable(true); // frame.setSize(450, 450); // frame.setVisible(true); // context.getWorkbenchFrame().addInternalFrame(frame); // =============== classify data ==============/ if (monitor.isCancelRequested()) { return null; } monitor.report(this.sClassifying); int[] classes = Classifier1D.classifyData(data, limits); // double[] classes = org.math.array.StatisticSample.one(data.length); // context.getWorkbenchFrame().warnUser("classification not yet implemented"); // =============== add field ==============/ if (monitor.isCancelRequested()) { return null; } monitor.report(sAddingField); FeatureDataset fd = null; ArrayList outData = new ArrayList(); FeatureSchema targetFSnew = null; int count = 0; Iterator iterp = fc.iterator(); String attname = this.selAttribute + "_" + this.selClassifier; while (iterp.hasNext()) { // count=count+1; // if(monitor != null){ // monitor.report("item: " + count + " of " + size); // } Feature p = (Feature) iterp.next(); Object val = p.getAttribute(this.selAttribute); if (val == null && !this.nullAsZero) continue; else count++; if (count == 1) { FeatureSchema targetFs = p.getSchema(); targetFSnew = FeatureSchemaTools.copyFeatureSchema(targetFs); if (targetFSnew.hasAttribute(attname)) { // attribute will be overwriten } else { // add attribute targetFSnew.addAttribute(attname, AttributeType.INTEGER); } } // -- evaluate value for every polygon Feature fcopy = FeatureSchemaTools.copyFeature(p, targetFSnew); // fcopy.setAttribute(this.selClassifier, new Integer(classes[count-1])); fcopy.setAttribute(attname, new Integer(classes[count - 1])); outData.add(fcopy); } fd = new FeatureDataset(targetFSnew); fd.addAll(outData); return fd; }