getting 0 for Node collection in pbf file


I’m trying to retrieve some data like a particular spot which are available in pbf like following :

	<node id="1345278334" lat="53.3912404" lon="-1.4685589" version="1" timestamp="2011-06-30T21:27:17Z" changeset="8594226" uid="358273" user="alpha2424">
		<tag k="barrier" v="block"/>

(Note : the above example is converted version of a pbf to osm)

the problem is I’m not able to see any data for Node collection but according to “osmformat.proto” <>

I should get some meaningful data back the code I’ve written

     if (h.getType().equals("OSMHeader"))
                        HeaderBlock hb = HeaderBlock.parseFrom(blobData);
                        System.out.println("hb: " + hb.getSource());
                    } else if (h.getType().equals("OSMData"))
                        PrimitiveBlock pb = PrimitiveBlock.parseFrom(blobData);
                        System.out.println("pb: " + pb.getGranularity());
                        Osmformat.StringTable st = pb.getStringtable();
                        System.out.println("s in pb : " + st.getSCount());
                        List<ByteString> sts = st.getSList();
                        for(int l=0;l<sts.size();l++)
                            System.out.println("SB : " + sts.get(l).toString());
                        List<Osmformat.PrimitiveGroup> pgs = pb.getPrimitivegroupList();
                        for (int i=0;i<pgs.size();i++)
                            System.out.println("ways :" + pgs.get(i).getWaysCount());
                            System.out.println("nodes :" + pgs.get(i).getNodesCount());
                            System.out.println("changes :" + pgs.get(i).getChangesetsCount());
                            System.out.println("relations :" + pgs.get(i).getRelationsCount());
                            Osmformat.DenseNodes dns = pgs.get(i).getDense();
                            System.out.println("denses :" + dns.getKeysValsCount());
                            List<Osmformat.Node> nodes =  pgs.get(i).getNodesList();

could anybody here help me what is my problem ? why does my Node collection is always returing 0 to me? but after converting it to OSM I can see that some of the nodes(not all of them) has some data such as above snippet xml?

Hi austinpowers,

unfortunately I cannot help with this because I usually parse PBF files byte by byte and do not use the PBF lib. Yes that’s a very strange way and I would not recommend it, unless you urgently need to speed-optimize the code to a very high degree.

What are you trying to do? Your source looks like a statistics query. There are some easier ways to get the numbers of nodes, ways and relations…

Hi Marqqs

Actually I want to read nodes information such as the user ID or if there are some extra information available like barriers or amenities so I want to take them into account but apparently pbf Lib is not helping me or I’m doing something wrong in my code.

the reason I start to count the Nodes is just because of probing to see if there is any kind of information available inside my particular pbf.

could you please give me some hints about your way of parsing pbf ? and it seems pbf files are compressed (zipped/gzipped/inflated/etc…)

Ok, I see! Thanks for the explanation!

You could use the framework Osmium. Alternatively, look into Osmium’s source code and copy that sections you need. As far as I know, Osmium is GPL licensed.

If you want to parse PBF bit by bit, look into the source of osmconvert. I think it’s the fastest, but I warn you: it’s chaotic. :wink:

Right, PBF is usually ZLIB compressed, but the compression is done block-internally. OSM-Wiki should help on this (article “PBF”).

“My way of parsing”? Just look into the source of osmconvert, I wrote it. But it’s really not a good example… sorry.