core icon indicating copy to clipboard operation
core copied to clipboard

PBF StreamSource to FeatureSource - 'Attribute id already exists!' when creating Feature list

Open digi-chris opened this issue 2 years ago • 3 comments

Hi all, hoping someone can help with this. I have some basic code that loads a PBF file as a PBFOsmStreamSource, uses a FilterBox to reduce the size of the search area and then converts this to a FeatureSource:

FileStream fs = File.OpenRead(filename);
var source = new PBFOsmStreamSource(fs);
var s = source.FilterBox(x1, y2, x2, y1);
filtered = from osmGeo in s
                where osmGeo.Type == OsmSharp.OsmGeoType.Node ||
                osmGeo.Type == OsmSharp.OsmGeoType.Way
                select osmGeo;

features = filtered.ToFeatureSource();
var featureList = features.ToList();

On small areas (say, about 1km), this code works fine.. but as soon as I make the FilterBox area fairly large, the last line of this code causes an exception, stating:

'Attribute id already exists!'

Any idea why this is happening? Is there some way I can filter out duplicate attribute ids?

Thanks!

Chris.

digi-chris avatar Jan 27 '23 12:01 digi-chris

Just to add - this is the stack trace:

   at NetTopologySuite.Features.AttributesTable.Add(String attributeName, Object attributeValue)
   at OsmSharp.Geo.DefaultFeatureInterpreter.TagsAndIdToAttributes(ICompleteOsmGeo osmObject)
   at OsmSharp.Geo.DefaultFeatureInterpreter.Interpret(ICompleteOsmGeo osmObject)
   at OsmSharp.Geo.Streams.Features.Interpreted.InterpretedFeatureStreamSource.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

Looking at what DefaultFeatureInterpreter.TagsAndIdToAttributes actually does, I'm wondering if there's something amiss with the PBF, with the specific area I'm looking at... but I don't know how to filter out the problem.

digi-chris avatar Jan 27 '23 16:01 digi-chris

OK, so I don't know if this helps, but in DefaultFeatureInterpreter.cs, line 523 and 524:

            var attr = osmObject.Tags.ToAttributeTable();
            attr.Add("id", osmObject.Id);

The osmObject.Tags attribute table already contains an Id, and on the next line it tries to add another, different, Id.

digi-chris avatar Jan 27 '23 19:01 digi-chris

Thought I would give an update on this - I was actually trying to draw in an incredibly large area - bigger than I'd realised.

I still think it should be possible, or at least the error should be trappable, but for my use-case I'd just miscalculated the size of the region, and once I got it down to what I actually wanted, the error didn't appear.

digi-chris avatar Jan 31 '23 00:01 digi-chris