Class Cast Exception

477 views
Skip to first unread message

Brian Oleksa

unread,
Aug 31, 2010, 8:07:26 AM8/31/10
to Java API for KML, br...@fwbnet.net

Hello All

Can somebody please tell me why I am getting a class cast
exception..??

java.io.IOException: java.lang.ClassCastException:
de.micromata.opengis.kml.v_2_2_0.Document cannot be cast to
de.micromata.opengis.kml.v_2_2_0.Placemark


final Kml kml = Kml.unmarshal(new File("test.kml"));
final Placemark placemark = (Placemark) kml.getFeature();

Point point = (Point) placemark.getGeometry(); // I get
the class cast exception here on this line

List<Coordinate> coordinates = point.getCoordinates();
for (Coordinate c : coordinates)
{
System.out.println(c.getLatitude());
System.out.println(c.getLongitude());
System.out.println(c.getAltitude());
}

Thanks in advanced.

Brian

Sphere

unread,
Aug 31, 2010, 11:21:20 AM8/31/10
to Java API for KML

Brian Oleksa

unread,
Aug 31, 2010, 11:42:53 AM8/31/10
to javaap...@googlegroups.com
Sphere

You mentioned in the little example that you sent me that I need to cast (Point)

Point point = placemark.getGeometry();  //need to cast (Point)

Then if I look at this example...I see that is casts Point to placemark.
http://labs.micromata.de/display/jak/HelloKML+%28Quickstart%29


Point point = (Point) placemark.getGeometry()

But your telling me that I cannot cast Placemark to Point..??

I am a little confused. Any more help is greatly appreciated.

I looked at the examples link that you sent me...and I do not see any examples of what I am trying to do.

Thanks,
Brian

Brian Oleksa

unread,
Aug 31, 2010, 3:35:40 PM8/31/10
to javaap...@googlegroups.com
Sphere

placemark.getGeometry() does appear to return a Point....so why can't you cast a placemark to a Point..??

Point point = (Point) placemark.getGeometry();    Why doesn't this work..??

I have been playing with this the last couple of days....any examples is greatly appreciated.

Sphere

unread,
Aug 31, 2010, 4:20:15 PM8/31/10
to Java API for KML
Hi Brian,

@Point point = (Point) placemark.getGeometry(); Why doesn't this
work..??

import:

import java.util.List;
import de.micromata.opengis.kml.v_2_2_0.Coordinate;
import de.micromata.opengis.kml.v_2_2_0.Point;
import de.micromata.opengis.kml.v_2_2_0.Kml;

I did check it, it works, and i've got the coordinates on my console.

Shpere

On 31 Aug., 21:35, Brian Oleksa <brian.ole...@gmail.com> wrote:
> Sphere
>
> placemark.getGeometry() does appear to return a Point....so why can't you
> cast a placemark to a Point..??
>
> Point point = (Point) placemark.getGeometry();    Why doesn't this work..??
>
> I have been playing with this the last couple of days....any examples is
> greatly appreciated.
>
> Thanks,
> Brian
>
> On Tue, Aug 31, 2010 at 11:42 AM, Brian Oleksa <brian.ole...@gmail.com>wrote:
>
> > Sphere
>
> > You mentioned in the little example that you sent me that I need to cast
> > (Point)
>
> > Point point = placemark.getGeometry();  //need to cast (Point)
>
> > Then if I look at this example...I see that is casts Point to placemark.
> >http://labs.micromata.de/display/jak/HelloKML+%28Quickstart%29
>
> > Point point = (Point) placemark.getGeometry()
>
> > But your telling me that I cannot cast Placemark to Point..??
>
> > I am a little confused. Any more help is greatly appreciated.
>
> > I looked at the examples link that you sent me...and I do not see any
> > examples of what I am trying to do.
>
> > Thanks,
> > Brian
>
> > On Tue, Aug 31, 2010 at 11:21 AM, Sphere <bahareh_sie...@yahoo.de> wrote:
>
> >> Hi,
>
> >> see
>
> >>http://labs.micromata.de/display/jak/KML+in+the+Java+world#KMLintheJa...

Brian Oleksa

unread,
Aug 31, 2010, 8:32:02 PM8/31/10
to javaap...@googlegroups.com
Shpere

I am not sure why it does not work. I get a class cast exception:


 java.io.IOException: java.lang.ClassCastException:
de.micromata.opengis.kml.v_2_2_0.Document cannot be cast to
de.micromata.opengis.kml.v_2_2_0.Placemark


Here is all my code:

I get a class cast exception on this line:

Point point = (Point) placemark.getGeometry(); 


import java.io.File;
import java.io.IOException;
import java.util.List;

import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.KmlFactory;
import de.micromata.opengis.kml.v_2_2_0.Placemark;
import de.micromata.opengis.kml.v_2_2_0.Point;
import de.micromata.opengis.kml.v_2_2_0.Coordinate;
import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Geometry;


public final class KMLTrackFactory extends TrackFactory<KMLTrackConfig>
{
    public KMLTrackFactory()
    {
        super("KML", KMLTrackConfig.class);
    }

    @Override
    public PointListTrack create(KMLTrackConfig config) throws IOException
    {
        try
        {

            final Kml kml = Kml.unmarshal(new File(config.getTrackFile()));
            return createFromGpx(config.getName()
, kml, config.getPointInterval());
        } catch (Exception ex)
        {
            throw new IOException(ex);
        }
    }


    PointListTrack createFromGpx(String name, Kml kml, long interval) throws IOException
    {
        ImmutableSortedMap.Builder<Long, Position> positions = ImmutableSortedMap.naturalOrder();

        long first_time = -1;
        long offset_time = 0;


        final Placemark placemark = (Placemark) kml.getFeature();
        Point point = (Point) placemark.getGeometry(); 
        List<Coordinate> coordinates = point.getCoordinates();
        for (Coordinate c : coordinates)
        {
            System.out.println(c.getLatitude());
            System.out.println(c.getLongitude());
            System.out.println(c.getAltitude());
            Position pos = Position.createPosition(c.getLatitude(), c.getLongitude(), c.getAltitude());

          positions.put(offset_time, pos);
        }

        return new PointListTrack(name, positions.build());
    }
}


Thanks again.

Brian


Brian Oleksa

unread,
Aug 31, 2010, 10:59:53 PM8/31/10
to javaap...@googlegroups.com
Sphere

I even ripped out all of my other code and I am just playing with the stuff you sent me. This does not work. It builds fine but when I run it I get the ClassCastException:

Exception in thread "main" java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Document cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark


Here is the code that fails:

import java.io.File;
import java.util.List;

import de.micromata.opengis.kml.v_2_2_0.Kml;

import de.micromata.opengis.kml.v_2_2_0.Placemark;
import de.micromata.opengis.kml.v_2_2_0.Point;
import de.micromata.opengis.kml.v_2_2_0.Coordinate;

public class KMLParser
{
    public static void main(String args[])
    {
        KMLParser parser = new KMLParser();
        parser.ParseKML();
    }

    public void ParseKML()
    {
        final Kml kml = Kml.unmarshal(new File("C:\\Projects\\argos\\kml\\test.kml"));


        final Placemark placemark = (Placemark) kml.getFeature();

        Point point = (Point) placemark.getGeometry();

        List<Coordinate> coordinates = point.getCoordinates();
        for (Coordinate c : coordinates)
        {
            System.out.println(c.getLatitude());
            System.out.println(c.getLongitude());
            System.out.println(c.getAltitude());
        }
    }
}

Any more help with this issue is greatly appreciated:

Sphere

unread,
Sep 1, 2010, 3:59:17 AM9/1/10
to Java API for KML
Hi Brian,

this is my code, which works:


import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import de.micromata.opengis.kml.v_2_2_0.Coordinate;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.Placemark;
import de.micromata.opengis.kml.v_2_2_0.Point;

public class testForBrian {

public void creatingKml() {

final Kml kml = new Kml();
kml.createAndSetPlacemark().withName("London,
UK").withOpen(Boolean.TRUE).createAndSetPoint().addToCoordinates(
-0.126236, 51.500152);

try {

kml.marshal(new File("HelloKml.kml"));

} catch (FileNotFoundException e) {

throw new RuntimeException(e);
}
}

public static void main(String[] args) throws FileNotFoundException,
Exception {

final Kml kmlNew = Kml.unmarshal(new File("HelloKml.kml"));

final Placemark placemark = (Placemark) kmlNew.getFeature();
Point point = (Point) placemark.getGeometry();
List<Coordinate> coordinates = point.getCoordinates();

for (Coordinate coordinate : coordinates) {

System.out.println(coordinate.getLatitude() + "\t" +
coordinate.getLongitude() + "\t" +coordinate.getAltitude() );
}
}
}

Sphere

On 1 Sep., 04:59, Brian Oleksa <brian.ole...@gmail.com> wrote:
> Sphere
>

Brian Oleksa

unread,
Sep 1, 2010, 4:13:02 PM9/1/10
to javaap...@googlegroups.com
Sphere

I found out where my problem was. In my KML file...there was a <LineString> and not a <Point>

So I just had to do this instead:

LineString lineString = (LineString) placemark.getGeometry();

Which leads me into my next question:

These are very basic test.kml files. What if you have a kml.file that has all the extra fun stuff in it.

For Example:

The <Document>, <Style> and <Icon> tags..??

Attached you will find a KML file that I am trying to parse the coordinates out of it.

If I ripped out all the extra stuff ...I was finally able to parse the coords from the kml file with the following code:


        final Placemark placemark = (Placemark) kml.getFeature();
        LineString lineString = (LineString) placemark.getGeometry();
        List<Coordinate> coordinates = lineString.getCoordinates();

        for (Coordinate c : coordinates)
        {

Brian Oleksa

unread,
Sep 1, 2010, 4:20:12 PM9/1/10
to javaap...@googlegroups.com
Sorry

I sent the last e-mail to quickly. I forgot to attache the file and complete my question:

So the main question is:

How do I get the coords out of the attached file..??

The attached file has extra tags in it (such as) <Document>, <Style> and <Icon>

If I try to parse it with this code:


        final Placemark placemark = (Placemark) kml.getFeature();
        LineString lineString = (LineString) placemark.getGeometry();
        List<Coordinate> coordinates = lineString.getCoordinates();
        for (Coordinate c : coordinates)

I get a class cast exception again.

Caused by: java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Document cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark

So it looks like I have to look for these extra tags....???

Anyway....I appreciate your help. I am moving in the right direction...!!!

Thanks,
Brian
test2.kml

Brian Oleksa

unread,
Sep 2, 2010, 10:10:21 AM9/2/10
to javaap...@googlegroups.com
Sphere

I get the same layout (in google earth) if I take out the Document and Style <tags> or leave them in.

Is there away to exclude these tags so I can just use my original code to parse out the coordinates..??

Or..do I have to account for these tags..??
Reply all
Reply to author
Forward
0 new messages