another technical blog...technically

Sunday, November 2, 2014

OpenMap to JTS and JTS to OpenMap

Yesterday i received a newsletter about OpenMap, and i remembered university times.
I also remembered i developed a small piece of code which helped me to convert OpenMap geomertries in JTS geometries.
This was useful because Oracle DBMS uses WKT format to define spatial data, but at that time, there weren't UI components able to render directly WKT.
Therefore, it was possible to convert WKT to JTS standard format, and then use JTS to map OpenMap objects. The code is below, i hope it could be still useful :-) .

OpenMapToJTS JTSToOpenMap
package presentationLevel.converters;


import java.util.List;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMPoint;
import com.bbn.openmap.omGraphics.OMPoly;
import com.bbn.openmap.proj.Ellipsoid;
import com.bbn.openmap.proj.coords.UTMPoint;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;

public class JTSToOpenMap {

 public static OMGraphicList createOMGraphicList(List l){
  OMGraphicList oml = new OMGraphicList();
  for (Geometry g : l)
   oml.add(createOMGraphic(g));
  return oml;
 }

 public static OMGraphic createOMGraphic(Geometry g){
  if (g instanceof Point)
   return createPoint((Point) g);
  else if (g instanceof LineString)
   return createLine((LineString) g);
  else if (g instanceof Polygon)
   return createPolygon((Polygon) g);
  else
   return null;
 }

 private static OMPoint createPoint(Point p){
  float[] coordinates = coordinatesConverter(p.getCoordinates());
  OMPoint omp = new OMPoint(coordinates[0], coordinates[1]);
  return omp;
 }

 private static OMPoly createLine(LineString l){
  OMPoly omp = new OMPoly(coordinatesConverter(l.getCoordinates()), OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_STRAIGHT);
  return omp;
 }

 private static OMPoly createPolygon(Polygon p){
  OMPoly omp = new OMPoly(coordinatesConverter(p.getCoordinates()), OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_STRAIGHT);
  omp.setIsPolygon(true);
  return omp;
 }

 private static float[] coordinatesConverter(Coordinate[] c){
  float[] coordinates = new float[(c.length)*2];
  for (int i = 0, j = 0; i < c.length; i++, j = j+2){
   LatLonPoint lp = UTMPoint.UTMtoLL(Ellipsoid.WGS_84, (float) c[i].y, (float) c[i].x, 19, false, null);
            coordinates[j] = lp.getLatitude();
            coordinates[j+1] = lp.getLongitude();
  }
  return coordinates;
 }

}
Share:
written in: Milano, Italia

0 commenti:

Post a Comment

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?