Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting building information from mapbox api

Is there a way to get building information (geometry, height etc) from the mapbox API?

I started from this example: https://www.mapbox.com/mapbox-gl-js/example/3d-buildings/ It adds a 3D layer on a map view. All I need is to get those information used to generate 3D building, to use in my application.

So I tried using this API: https://www.mapbox.com/api-documentation/#retrieve-features-from-vector-tiles

For example, if I call this:

https://api.mapbox.com/v4/mapbox.mapbox-streets-v7/tilequery/-74.0066,40.7135.json?radius=50&limit=50&access_token=

I get various informartion but nothing related to buildings.

According to this: https://www.mapbox.com/blog/mapbox-studio-building-heights/

the information should be there somewhere

like image 665
lviggiani Avatar asked Feb 25 '26 20:02

lviggiani


1 Answers

I've found the solution:

// Dafault public token, replace with yours if you have one
mapboxgl.accessToken = 'pk.eyJ1IjoibHZpZ2dpYW5pIiwiYSI6ImNpeHZvbGVqMzAwMGoyd3J5YXllbnpuOHQifQ.RAyB0ZTsnLggAZYp_TPmHQ';

var map = new mapboxgl.Map({
    container: div,
    style: 'mapbox://styles/mapbox/outdoors-v9',
    interactive: false
});

map.fitBounds(
    someBounds, // arbitrary bounds 
    {
        linear: true
    });

map.on("load", function(){
    features = map.queryRenderedFeatures(
            { layers: ["building"], filter: ['==', 'extrude', 'true']}); // This is where I get building information

    features.forEach(function(feature){
        console.log(feature.geometry);  // feature.geometry getter returns building shape points (basement)
        console.log(feature.properties.height); // this is the building height
        console.log(feature.properties.min_height); // this is the building part elevation from groung (e.g. a bridge)
    });
});
like image 101
lviggiani Avatar answered Feb 27 '26 09:02

lviggiani