2

I need to display the coordinates of the grid lines. I saw this article 'Fun with Graticules', which has much common with that I want to do.

But I still have a question. How did you create the new instance of the CoordinateFormatter class in GeoTools?

And also, how did you overload this setAttributes function? -

GridFeatureBuilder grid_builder =
       new GridFeatureBuilder(GridType) {  
            @Override
            public void setAttributes(GridElement ge, Map<String, Object> map) {
                 some code...       
            }
};
  • thanks for reminding me of that code, I must chase up the licence some day. – Ian Turton Feb 8 at 11:30
0

I created a class LineFeatureBuilder that extends GridFeatureBuilder:

import java.text.Format;
import java.util.Map;

import org.geotools.grid.GridElement;
import org.geotools.grid.GridFeatureBuilder;
import org.geotools.grid.ortholine.LineOrientation;
import org.geotools.grid.ortholine.OrthoLine;
import org.geotools.measure.CoordinateFormat;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.cs.CoordinateSystem;

public class LineFeatureBuilder extends GridFeatureBuilder {
  /** Default feature TYPE name: "linegrid" */
  public static final String DEFAULT_TYPE_NAME = "linegrid";

  /** Name used for the integer id attribute: "id" */
  public static final String ID_ATTRIBUTE_NAME = "id";

  /**
   * Name of the Integer level attribute ("level")
   */
  public static final String LEVEL_ATTRIBUTE_NAME = "level";

  /**
   * Name of the Object value attribute ("value")
   */
  public static final String VALUE_ATTRIBUTE_NAME = "value";

  public static final String VALUE_LABEL_NAME = "label";

  protected int id;
  private SimpleFeatureType type;
  private CoordinateFormat formatter;
  private Format xFormat;
  private Format yFormat;
  private String xUnit;
  private String yUnit;

  public LineFeatureBuilder(SimpleFeatureType type2) {
    super(type2);
    this.type = type2;
    formatter = new CoordinateFormat();
    formatter.setNumberPattern("##0.00");
    formatter.setAnglePattern("DD.dd");

    formatter.setCoordinateReferenceSystem(type.getCoordinateReferenceSystem());
    xFormat = formatter.getFormat(0);
    yFormat = formatter.getFormat(1);
    CoordinateSystem coordinateSystem = type.getCoordinateReferenceSystem().getCoordinateSystem();
    xUnit = "" + coordinateSystem.getAxis(0).getUnit().toString();
    yUnit = "" + coordinateSystem.getAxis(1).getUnit().toString();
  }

  @Override
  public void setAttributes(GridElement el, Map<String, Object> attributes) {
    if (el instanceof OrthoLine) {
      OrthoLine orthoLine = (OrthoLine) el;
      attributes.put(ID_ATTRIBUTE_NAME, ++id);
      attributes.put(LEVEL_ATTRIBUTE_NAME, orthoLine.getLevel());

      Coordinate v0 = orthoLine.getVertices()[0];
      Double value = null;
      String label = "";
      if (orthoLine.getOrientation() == LineOrientation.HORIZONTAL) {
        value = v0.y;
        label = yFormat.format(v0.y) + yUnit;
      } else {
        value = v0.x;
        label = xFormat.format(v0.x) + xUnit;
      }
      attributes.put(VALUE_ATTRIBUTE_NAME, value);
      attributes.put(VALUE_LABEL_NAME, label);

    } else {
      throw new IllegalArgumentException("Expected an instance of OrthoLine");
    }
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.