Re: [OpenMap Users] Help with OMGraphics

From: Valerio Valrosso <valrosso.valerio_at_gmail.com>
Date: Wed, 2 Sep 2009 13:02:25 +0200

Hi Tore, i also write this class to make conversions i need. I post the code
that could maybe be useful even if less complete than yours ;)

package presentationLevel.filters.converters;

import java.util.List;

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.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<Geometry> l){
        OMGraphicList oml = new OMGraphicList();
        for (Geometry g : l)
            if (g instanceof Point)
                oml.add(createPoint((Point) g));
            else if (g instanceof LineString)
                oml.add(createLine((LineString) g));
            else if (g instanceof Polygon)
                oml.add(createPolygon((Polygon) g));
        return oml;
    }

    private static OMPoint createPoint(Point p){
        Coordinate coordinates = p.getCoordinate();
        OMPoint omp = new OMPoint(((float) coordinates.x), ((float)
coordinates.y));
        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){
            coordinates[j] = ((float) c[i].x);
            coordinates[j+1] = ((float) c[i].y);
        }
        return coordinates;
    }
}


2009/9/2 Tore Halset <halset_at_pvv.ntnu.no>

> 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"]
>


--
[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 - 07:02:55 EDT

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