2

So I'm currently working on Deck.gl and React and I need to load a geojson file which has an additional property named "floors" or something similar which tells how many floors the building has.

Is there any way to extrude alternating floors horizontally just a little bit so that it looks like a floor edge like some of the buildings in this image (although most of them just go thinner at the top). I tried searching the deck.gl but there is no such thing. I looked up and found MapBox-gl-js has something called an extrusion-base-height which lets you add polygon above another but there is no such thing as extruding horizontally to make 1 floor thinner and then back to the original size. This would give and edge whenever a new floor starts.

I have scoured the docs for deck.gl but couldn't find any thing on extruding horizontally or in another sense changing the polygon area/size so that I can draw multiple size polygons on the same spot.

enter image description here

Another clear picture of what I'm trying

enter image description here

Things I want to do.

  1. The red polygon is tilted. Need to make it's orientation the same as the green one and reducing it's area at the same time.

  2. Move the red polygon base at the top of the green polygon.

The test data I'm using is given below,

var offset = 0.00001;
    var data = [{
        polygon: [
                    [-77.014904,38.816248],
                    [-77.014842,38.816395],
                    [-77.015056,38.816449],
                    [-77.015117,38.816302],
                    [-77.014904,38.816248]                      
                ],
        height: 30
    },
    {
        polygon: [
                    [-77.014904 + offset ,38.816248],
                    [-77.014842 - offset, 38.816395 - offset],
                    [-77.015056 - offset, 38.816449 - offset],
                    [-77.015117 + offset, 38.816302],
                    [-77.014904 + offset, 38.816248]
                ],          
        height: 40
    }

    ];

EDIT:- I think the proper way would be to convert longitude/latitude to Cartesian Coordinates, get the vectors to the 4 corners of the polygon translate the vectors to move towards the center by the offset amount then convert back. But this would only work with quad/rectangle polygon, for buildings that are made up of multiple quads I'd need another way.

gallickgunner
  • 472
  • 5
  • 20
  • Can you explain what you want the result to look like exactly? Not sure what you mean by "like a floor edge". – Steve Bennett Jul 25 '19 at 05:29
  • @SteveBennett - look at the updated picture. See how there is another smaller polygon on top of the main building? I want to create that. Currently I'm trying to change the coordinates of the polygon to make a smaller one but I can't seem to find a map between the longitude latitude and the polygon coordinates (i.e from which coordinate to subtract/add to make a smaller one) – gallickgunner Jul 25 '19 at 07:50

1 Answers1

2

If I'm understanding correctly, your problem boils down to: given a polygon (the footprint of the lower part of the building), generate a slightly smaller version of the same polygon, centered within it.

Fortunately, this is really easy using Turf's transformScale method.

So your steps will be:

  1. Convert your polygon data into GeoJSON. (Which I assume you have some mechanism to do, in order to display it in Mapbox-GL-JS in the first place.)
  2. Generate a smaller polygon using turf.transformScale(base, 0.9)
  3. Add the new polygon with map.addSource
  4. Display the new polygon with map.addLayer, setting the extrusion base height etc as required.
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • Holy that was a god send. Quick question. I'm quite new to all of this stuff so I can't wrap my head around optimizations. If I like have the building data for a small city it's gonna be a ton of buildings with a ton of flloors. Is it okay to add that many layers? – gallickgunner Jul 26 '19 at 06:08
  • There will definitely be a number of buildings and layers for which it is *not* ok :) At some point you will have to look at smarter ways of generating and managing data, like generating vector tiles offline. – Steve Bennett Jul 31 '19 at 04:29
  • Hmm for now I'm reading the original data then made copies for the floor data in an expanded array... Then I draw the original building (outlines) with one layer and the floors with another. This is gonna cost much more memory but better than adding a layer for every floor – gallickgunner Jul 31 '19 at 17:18