Is it able to retrieve street from a named city from an OSM PBF file?

Hello,

Is Aspose.GIS able to retrieve all of the street name using solely the name of a City, without any bounding box of some sorts?

Thanks

Hi, @Xhat

Yes it is possible. Consider the below code.

using (var layer = VectorLayer.Open(citiesPath, Drivers.GeoJson))
{
    // Select features based on values of the attribute. This code enumerates all features in the layer
    // and selects all features that match a condition.
    var features = layer.WhereEqual("city_name", "London");

    // Print results.
    Console.WriteLine("London Streets");
    foreach (var feature in features)
    {
        var name = feature.GetValue<string>("street_name");
        Console.WriteLine(name);
    }
    Console.WriteLine();
}

The result will contain the streets duplicates. You can remove duplicates if you collect the streets into an array and use the Distinct method from LINQ

For more examples, please go to the Filtering and Indexing page

Best Regards.

Thanks a lot!

Hello,

The following code will crash on OSM XML map that you can download at https://download.geofabrik.de/europe/france/aquitaine-latest.osm.bz2

“The given key was not inside the dictionnary” is the exception being thrown.

Surrounding it with a try catch will net some results but with a lot of missing streets.

var streetList = new List();
using (var layer = VectorLayer.Open(city.Value, Drivers.OsmXml))
{
var streetLayers = layer.WhereSet(“addr:street”).WhereSet(“addr:postcode”);

                //try
                //{
                    foreach (var streetLayer in streetLayers)
                    {
                        var streetName = streetLayer.GetValue<string>("addr:street");
                        var zipCode = streetLayer.GetValue<string>("addr:postcode");

                        var address = new Address { StreetName = streetName, ZIPCode = zipCode };

                        if (!streetList.Contains(address))
                            streetList.Add(address);
                    }
                //}
                //catch (KeyNotFoundException ex)
                //{ }
            }

Can you check it? I can’t upload anything due to poor internet where I am. I’ll try to find something if you can’t reproduce it.

Thanks

Hi, @Xhat

I found a ‘space’ char in the names of the attributes when insert your sample in Visual Studio. I have reworked a code and found 6520 street. Please check the below code.

// Dictionary works faster than List
var streets = new Dictionary<string, string>();

// You should not use "space" between words.
var streetField = "addr:street";
var postcodeField = "addr:street";

// Validate the field names
if (!layer.Attributes.Contains(streetField))
    throw new ArgumentException($"Filed not found: {streetField}");
if (!layer.Attributes.Contains(postcodeField))
    throw new ArgumentException($"Filed not found: {postcodeField}");

// Collect the streets
var streetLayers = layer.WhereSet(streetField).WhereSet(postcodeField);
foreach (var streetLayer in streetLayers)
{
    var streetName = streetLayer.GetValue<string>(streetField);
    var zipCode = streetLayer.GetValue<string>(postcodeField);

    if (!streets.ContainsKey(streetName))
        streets.Add(streetName, zipCode);
} 

Best Regards.