Re: [OpenMap Users] Help with OMGraphics

From: Tore Halset <halset_at_pvv.ntnu.no>
Date: Wed, 2 Sep 2009 11:40:37 +0200

Hello.

I have done some work to convert JTS geometries to OMGraphic. I think
it would be nice to have this included in OpenMap as I guess JTS is
used in most java applications working on geometries.

Regards,
  - Tore.

import com.bbn.openmap.omGraphics.OMAreaList;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMPoly;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;

public class JTSToOMGraphic {

     public static OMPoly toOmPoly(LinearRing ring) {
         return toOmPoly(ring.getCoordinateSequence());
     }

     public static OMPoly toOmPoly(LineString lineString) {
         return toOmPoly(lineString.getCoordinateSequence());
     }

     private static OMPoly toOmPoly(CoordinateSequence cs) {
         int n = cs.size();
         double[] llps = new double[n * 2];
         Coordinate c = new Coordinate();
         for (int i = 0; i < n; i++) {
             cs.getCoordinate(i, c);
             llps[i * 2] = c.y;
             llps[(i * 2) + 1] = c.x;
         }
         OMPoly p = new OMPoly(llps, OMGraphic.DECIMAL_DEGREES,
OMGraphic.LINETYPE_STRAIGHT);
         return p;
     }

     public static OMGraphic toOMGraphic(Polygon p) {
         if (p.getNumInteriorRing() == 0) {
             return toOmPoly(p.getExteriorRing());
         } else {
             OMAreaList a = new OMAreaList(p.getNumGeometries());
             a.setConnectParts(false);
             a.add(toOmPoly(p.getExteriorRing()));
             for (int i = 0; i < p.getNumInteriorRing(); i++) {
                 a.add(toOmPoly(p.getInteriorRingN(i)));
             }
             return a;
         }
     }

     public static OMGraphic toOMGraphic(MultiPolygon mp) {
         if (mp.getNumGeometries() == 1) {
             return toOMGraphic((Polygon) mp.getGeometryN(0));
         } else {
             OMGraphicList gl = new
OMGraphicList(mp.getNumGeometries());
             for (int i = 0; i < mp.getNumGeometries(); i++) {
                 Polygon p = (Polygon) mp.getGeometryN(i);
                 gl.add(toOMGraphic(p));
             }
             return gl;
         }
     }

     public static OMGraphic toOMGraphic(GeometryCollection gc) {
         int n = gc.getNumGeometries();
         OMGraphicList gl = new OMGraphicList(n);
         for (int i = 0; i < n; i++) {
             gl.add(toOMGraphic(gc.getGeometryN(i)));
         }
         return gl;
     }

     public static OMGraphic
toOMGraphic(com.vividsolutions.jts.geom.LineString ls) {
         return toOmPoly(ls.getCoordinateSequence());
     }

     public static OMGraphic toOMGraphic(Geometry g) {
         if (g instanceof com.vividsolutions.jts.geom.Polygon) {
             return toOMGraphic((com.vividsolutions.jts.geom.Polygon)
g);
         }
         if (g instanceof MultiPolygon) {
             return toOMGraphic((MultiPolygon) g);
         }
         if (g instanceof GeometryCollection) {
             return toOMGraphic((GeometryCollection) g);
         }
         if (g instanceof com.vividsolutions.jts.geom.LineString) {
             return
toOMGraphic((com.vividsolutions.jts.geom.LineString) g);
         }
         throw new IllegalArgumentException("does not support geometry
of type "
                 + g.getGeometryType() + ": " + g.toText());
     }

}

On Sep 1, 2009, at 15:04 , Valerio Valrosso wrote:

> Thanks for the very fast reply. We have so many doubts about the
> conversion, i've given a look to the OMGraphicHandlerLayer, but i
> still
> not undestand how to make the conversion.
> For example, if i have a Point, which is a JTS Geometry, how can i
> convert it to a OMGraphic point?
> Is there a method or a specific library that contains some methods
> which
> do the job?
> Or maybe we have to retrieve latitude and longitude from the JTS
> Geometry and create and instance og OMGraphic?
> Moreover, if we have to convert a Line, and not a Point, there is a
> corrispondence between formats?
> You'll understand we are confused about this project, i hope you can
> clear our doubts.
> Thank you for helping us.
>
> Valerio & Niki
>
> Il giorno mar, 01/09/2009 alle 13.10 +0200, "Carsten Ø. Madsen" ha
> scritto:
>> Hello Valerio
>>
>> Create a new layer that extends OMGraphicHandlerLayer which converts
>> your JTS objects into the corresponding OMGraphic's and return those
>> objects in the prepare method.
>>
>> Take a look at the prepare method here for an example
>> http://carsten-oland.blogspot.com/2009/08/build-you-own-gpsgis-system-in-less.html
>>
>> BR
>> Carsten
>>
>> On 01-09-2009 12:52, Valerio Valrosso wrote:
>>> Dear OpenMap team,
>>> we need help using OMGraphics for a university project. We are
>>> loading some layers from oracle db, converting them in JTS object in
>>> the domain level. Now we need to represent them with OpenMap at the
>>> presentation level, but, even if we are looking for a possible
>>> solution since 1 week, we don't have found a good solutions.
>>> We ask you some advices to solve this problems, or maybe a
>>> workaround to mantain data source indipendence.
>>> I'm sorry for the bad english, i hope you've understood our problem.
>>> Regards,
>>> Valerio
>>>
>>>
>>
>
> --
> [To unsubscribe to this list send an email to "majdart_at_bbn.com"
> with the following text in the BODY of the message "unsubscribe
> openmap-users"]
>

--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Wed Sep 02 2009 - 06:08:38 EDT

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:09 EDT