public void draw(
      BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double dpi)
      throws ReadException {
    double scale = viewPort.getScale();
    //		double fontScaleFactor = FConstant.FONT_HEIGHT_SCALE_FACTOR;

    SimpleTextSymbol sym = new SimpleTextSymbol();

    sym.setFont(getFont());

    sym.setUnit(unit);
    sym.setReferenceSystem(referenceSystem);
    if (zoom == null
        || (zoom.isUserDefined()
            && (scale >= zoom.getMaxScale())
            && (scale <= zoom.getMinScale()))) {
      FeatureSet set = null;
      DisposableIterator iterator = null;
      try {
        // limit the labeling to the visible extent
        ArrayList fields = new ArrayList();
        int heightPos = -1;
        int rotationPos = -1;
        int textPos = -1;
        int colorPos = -1;
        int geomPos = -1;

        if (!this.usesFixedSize()) {
          if (getHeightField() != null) {
            heightPos = fields.size();
            fields.add(getHeightField());
          }
        }
        if (getRotationField() != null) {
          rotationPos = fields.size();
          fields.add(getRotationField());
        }
        if (getTextField() != null) {
          textPos = fields.size();
          fields.add(getTextField());
        }

        if (!this.usesFixedColor() && getColorField() != null) {
          colorPos = fields.size();
          fields.add(getColorField());
        }

        FeatureStore featureStore = layer.getFeatureStore();

        geomPos = fields.size();
        String geomName = featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
        fields.add(geomName);

        FeatureQuery featureQuery = featureStore.createFeatureQuery();

        featureQuery.setAttributeNames((String[]) fields.toArray(new String[fields.size()]));
        // TODO no set filter y layer is contained totaly in viewPort
        ContainsEnvelopeEvaluator iee =
            new ContainsEnvelopeEvaluator(
                viewPort.getAdjustedExtent(),
                viewPort.getProjection(),
                featureStore.getDefaultFeatureType(),
                geomName);
        featureQuery.setFilter(iee);

        set = featureStore.getFeatureSet(featureQuery);

        //				ReadableVectorial source = layer.getSource();
        //				SelectableDataSource recordSet = source.getRecordset();
        iterator = set.fastIterator();
        CreateLabelsOperationContext cloc = new CreateLabelsOperationContext();
        cloc.setDublicates(true);
        cloc.setPosition(0);
        while (iterator.hasNext()) {
          if (cancel.isCanceled()) {
            return;
          }
          Feature feature = (Feature) iterator.next();
          //				for(int i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
          //					Value[] vv = recordSet.getRow(i);
          double size;
          Color color = null;
          if (useFixedSize) {
            // uses fixed size
            size = fixedSize; // * fontScaleFactor;
            //					} else if (idHeightField != -1) {
          } else if (heightFieldName != null) {
            // text size is defined in the table
            try {
              //							Object obj=feature.get(idHeightField);
              Object obj = feature.get(heightPos);
              if (obj != null) {
                size = ((Number) obj).doubleValue(); // * fontScaleFactor;
              } else {
                size = 0;
              }
            } catch (ClassCastException ccEx) {
              //							if (!NullValue.class.equals(feature.get(idHeightField).getClass())) {
              if (!NullValue.class.equals(feature.get(heightPos).getClass())) {

                throw new ReadException("Unknown", ccEx);
              }
              // a null value
              //							Logger.getAnonymousLogger().
              //								warning("Null text height value for text
              // '"+feature.get(idTextField).toString()+"'");
              Logger.getAnonymousLogger()
                  .warning(
                      "Null text height value for text '" + feature.get(textPos).toString() + "'");

              continue;
            }
          } else {
            // otherwise will use the size in the symbol
            size = sym.getFont().getSize();
          }

          size =
              CartographicSupportToolkit.getCartographicLength(
                  this, size, viewPort, MapContext.getScreenDPI());
          //													  dpi);
          //								toScreenUnitYAxis(this,
          //												  size,
          //												  viewPort
          //												 );

          if (size <= MIN_TEXT_SIZE) {
            // label is too small to be readable, will be skipped
            // this speeds up the rendering in wider zooms
            continue;
          }

          sym.setFontSize(size);

          if (useFixedColor) {
            color = fixedColor;
            //					} else if (idColorField != -1) {
          } else if (colorFieldName != null) {
            // text size is defined in the table
            try {
              //							color = new Color(feature.getInt(idColorField));
              color = new Color(feature.getInt(colorPos));
            } catch (ClassCastException ccEx) {
              //							if (feature.get(idColorField) != null) {
              if (feature.get(colorPos) != null) {
                throw new ReadException("Unknown", ccEx);
              }
              // a null value
              //							Logger.getAnonymousLogger().
              //								warning(
              //									"Null color value for text '"
              //											+ feature.getString(idTextField)
              //											+ "'");
              Logger.getAnonymousLogger()
                  .warning("Null color value for text '" + feature.getString(textFieldName) + "'");

              continue;
            }
          } else {
            color = sym.getTextColor();
          }

          sym.setTextColor(color);

          double rotation = 0D;
          //					if (idRotationField!= -1) {
          if (rotationFieldName != null) {
            // text rotation is defined in the table
            //						rotation = -Math.toRadians(((Number)
            // feature.get(idRotationField)).doubleValue());
            rotation = -Math.toRadians(((Number) feature.get(rotationPos)).doubleValue());
          }

          Geometry geom = feature.getDefaultGeometry();
          //					Object obj=feature.get(idTextField);
          Object obj = feature.get(textPos);
          if (obj != null) {
            sym.setText(obj.toString());
          }
          sym.setRotation(rotation);

          FLabel[] aux = (FLabel[]) geom.invokeOperation(CreateLabels.CODE, cloc);
          //					FLabel[] aux = geom.createLabels(0, true);
          for (int j = 0; j < aux.length; j++) {
            Point p =
                geomManager.createPoint(
                    aux[j].getOrig().getX(), aux[j].getOrig().getY(), SUBTYPES.GEOM2D);
            p.transform(viewPort.getAffineTransform());
            if (properties == null) {
              sym.draw(g, null, p, cancel);
            } else {
              sym.print(g, null, p, properties);
            }
          }
        }

      } catch (GeometryOperationNotSupportedException e) {
        throw new ReadException("Could not draw annotation in the layer.", e);
      } catch (GeometryOperationException e) {
        throw new ReadException("Could not draw annotation in the layer.", e);
      } catch (BaseException e) {
        throw new ReadException("Could not draw annotation in the layer.", e);
      } finally {
        if (iterator != null) {
          iterator.dispose();
        }
        if (set != null) {
          set.dispose();
        }
      }
    }
  }
Пример #2
0
 public Object getValue(Feature feature, String nameField) {
   if (feature != null) return feature.get(nameField);
   return null;
 }