Opened 14 years ago

Closed 11 years ago

#2772 closed defect (fixed)

Interleaved feature adding to different layers is not possible with KML OGR driver

Reported by: moellney Owned by: condit
Priority: normal Milestone: 1.9.0
Component: OGR_SF Version: 1.6.0
Severity: normal Keywords: KML
Cc: warmerdam

Description

I came to a problem with the creation of layers.

I have to calculate positions and at these positions different features should be added. I would like to add the features to different layers to better select later, what to see and what not....

So I mainly have (pseudo code)

pLayer1 = create layer1();
pLayer2 = create layer2();

for (i = 1; i<1000; i++)
{
   pos = calc_position (i);
   pLayer1.addFeature(createFeature1(pos));
   pLayer2.addFeature(createFeature2(pos));
}

Is this ment to be supported If so, the implementation of KML export is broken, because with the creation of a new layer the output of the other layer is closed.

There is no buffering of the layer context until the datasource is closed...

Or do i really have to write two for loops:

pLayer1 = create layer1();
for (i = 1; i<1000; i++)
{
   pos = calc_position (i);
   pLayer1.addFeature(createFeature1(pos));
}

pLayer2 = create layer2();
for (i = 1; i<1000; i++)
{
   pos = calc_position (i);
   pLayer2.addFeature(createFeature2(pos));
}

Change History (8)

comment:1 by Even Rouault, 14 years ago

moellney,

your analysis of what happens in the driver is right. You cannot mix creation of features like in your first code snippet. Your second code snippet is the only supported way. This limitation is due to the nature of the KML format itself.

I don't think that there is an easy way of making your first snippet work without storing all features in memory and differing their writing at the time the datasource is closed, which is not what we want to do if the user writes lots of features...

What could be done of course is to detect that we write again in a layer that has been "closed" by the writing into another layer and throw an appropriate warning.

comment:2 by warmerdam, 14 years ago

Cc: warmerdam added
Component: defaultOGR_SF
Owner: changed from warmerdam to condit

I agree with Even. Reassigning to Chris to improve error handling in the future.

in reply to:  1 comment:3 by moellney, 14 years ago

Replying to rouault:

moellney,

[...]

What could be done of course is to detect that we write again in a layer that has been "closed" by the writing into another layer and throw an appropriate warning.

I think this is the implicit reason for my first attempt (interleaved creation of layer features). I was looking for a "closeLayer" method and could not find one. This lead me to the assumption that it might be possible.

comment:4 by warmerdam, 14 years ago

In fact, for some drivers it is perfectly fine to interleave writes to different layers. Other drivers (especially serial text formats like KML) do not support this. Currently there is no way to ask the driver whether it is allowed or not so conservative applications will generally avoid doing this.

comment:5 by condit, 14 years ago

Status: newassigned

Sadly this is a limit of the serial XML output. The correct solution is to re-implement the driver using libkml, which would allow this to work without a problem... I've started work on that but haven't had time to finish up.

comment:6 by warmerdam, 14 years ago

Chris,

I was not suggesting that you should add support for interleaved writes, just that you add an error if write are done out of order.

comment:7 by condit, 14 years ago

It would be good to support interleave writes, though, and libkml will do that. In the meantime I'll look into adding an error to the existing code.

comment:8 by Even Rouault, 11 years ago

Milestone: 1.9.0
Resolution: fixed
Status: assignedclosed

r22772 /trunk/ (4 files in 2 dirs): KML: emit proper error when trying to write features interleaved in several layers (#2772)

Note: See TracTickets for help on using tickets.