[OpenLayers-Users] Best way to read/parse kml

Christopher Schmidt crschmidt at metacarta.com
Fri Jul 17 09:44:32 EDT 2009


On Fri, Jul 17, 2009 at 09:21:40AM +0200, Vidal, Antoni wrote:
> Hi,
> 
> Added a sample kml.
> 
> To understand the error I post my code:
> 
>         features = g.read(req.responseText);
>         
>         var points = new Array();
>         for(var i=0; i<features.length; ++i) {
>           ls = features[i].geometry;
> 
> 	var ifls = ls.toString();
> 
> 	if(ifls.startsWith('LINESTRING')){
>           for(m=0;m<ls.components.length;m++){
>     		OpenLayers.Projection.transform(ls.components[m], src, dest);
> 	    	points.push(new OpenLayers.Geometry.Point(ls.components[m].x, ls.components[m].y));
> 			}
>         }else if(ifls.startsWith('POINT')){
> 		OpenLayers.Projection.transform(ls, src, dest);
> 			
> 		/********** Pop up **********/
>             	popup = new OpenLayers.Popup.FramedCloud("chicken", 
>                 	new OpenLayers.LonLat(ls.x,ls.y),
>                         null,
>                         "<div style='font-size:.8em'>"+html+"</div>",
>                         null, true, null);
>             		map.addPopup(popup);		
> 		// ********* End Popup******/
> 			
>         }
> 
> And the error got using firebug: ifls is null
> 
> It fails the line features[i].geometry because g.read returns a OL.Features.Vector, and I supose that don't parse MultiGeometry, but I need to get every point of the geometries to transform to another srs.

It does parse MultiGeometry. The problem is that ultiGeometries don't
have a toString(), becuase there is no valid WKT representation of them.

However, your code is far more complex than it should be. 

1. Geometries can be transformed, you don't have to transform individual
points.

   ls.transform(fromProj, toProj);
 
Will transform the geometry in place.  

2. Don't check the WKT to figure out type. Instead, check the
CLASS_NAME:

  if (ls.CLASS_NAME=="OpenLayers.Geometry.LineString") {
     // etc.
  }

-- Chris

> Thanks a lot.
> 
> Antoni Vidal
> Unitat d'Aplicacions SIG-WEB
> Ext. 3228
> 
> -----Missatge original-----
> De: bartvde at osgis.nl [mailto:bartvde at osgis.nl] 
> Enviat: viernes, 17 de julio de 2009 9:12
> Per a: Vidal, Antoni
> A/c: users at openlayers.org
> Tema: Re: [OpenLayers-Users] Best way to read/parse kml
> 
> Hi,
> 
> can you post a sample KML file which fails?
> 
> What is the exact error that you are getting?
> 
> Best regards,
> Bart
> 
> > Hello,
> >
> >
> >
> > I'm trying to read kml file from user client side. I use:
> >
> >  g =  new OpenLayers.Format.KML();
> >
> > features = g.read(req.responseText);
> >
> > ...
> >
> >
> >
> > To get the collection of features, and all is ok if the kml NOT contains a
> > <MultiGeometry> tag. If so, I get an error.
> >
> >
> >
> > How can I solve this?
> >
> >
> >
> > Thank's in advance
> >
> >
> >
> > Antoni Vidal
> >
> > Unitat d'Aplicacions SIG-WEB
> > Institut Cartogràfic de Catalunya
> > <http://mercuri.icc.cat/website/mob_nf/mob1/mob2/inici2.htm?CONSULTA=Institut%20Cartogr%25E0fic%20de%20Catalunya&XYADDRESS=429486:4580392>
> > Parc de Montjuïc, E-08038 Barcelona
> > Tel. (+34) 93 567 15 00 (ext. 3228)
> > www.icc.cat <http://www.icc.cat/>
> >
> >
> >
> >
> >
> >
> >
> > _______________________________________________
> > Users mailing list
> > Users at openlayers.org
> > http://openlayers.org/mailman/listinfo/users
> >
> 


> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users




More information about the Users mailing list