0

I am pretty new to GIS as a whole. I have a simple flat file in a csv format, as an example:

lat,long,name,val
25,40,abc,10
50,44,def,20

What I am trying to achieve is to assign a 3d polygon in Mapbox such as in this example. While the settings might be quite straight forward in Mapbox where you assign a height and color value based on a data range, it obviously does not work in my case.

I think I am missing out other files such as mentioned in the blog post, like shapefiles or some other file that is required to assign 3d layouts to the 3d extrusion.

I need to know what I am missing out in configuring a 3d polygon, say a cube in Mapbox based on the val data column in my CSV.

0

So I figured what I was missing was the coordinates that make up the polygons I want to display. This can easily be defined in a geojson file format, if you are interested in the standards, refer here. For the visual I need, I would require:

  1. Points (typically your long and lat coordinates)
  2. Polygon (a square would require 5 vertices, the lines connecting and defining your polygon)
  3. Features (your data points)
  4. FeatureCollection (a collection of features)

This are all parts of the geojson format, I used Python and its geojson module which comes with everything I need to do the job.


Using a helper function below, I am able to compute square/rectangular boundaries based on a single point. The height and width defines how big the square/rectangle appears.

def create_rec(pnt, width = 0.00005, height = 0.00005):  
    pt1 = (pnt[0] - width, pnt[1] - height)  
    pt2 = (pnt[0] - width, pnt[1] + height)  
    pt3 = (pnt[0] + width, pnt[1] + height)  
    pt4 = (pnt[0] + width, pnt[1] - height)  
    pt5 = (pnt[0] - width, pnt[1] - height)  

    return Polygon([[pt1,pt2,pt3,pt4,pt5]]) #assign to a Polygon class from geojson

From there it is pretty straight forward to append them into list of features, FeatureCollection and output as a geojson file:

with open('path/coordinates.csv', 'r') as f:
    headers = next(f)
    reader = csv.reader(f)
    data = list(reader)

transform = []

for i in data:
    #3rd last value is x and 2nd last is the y
    point = Point([float(i[-3]), float(i[-2])]) 
    polygon = create_rec(point['coordinates'])

    #in my case I used a collection to store both points and polygons
    col = GeometryCollection([point, polygon])

    properties = {'Name':i[0]}

    feature = Feature(geometry = col, properties = properties)
    transform.append(feature)

fc = FeatureCollection(transform)

with open('target_doc_u.geojson', 'w') as f:
    dump(fc, f)

The output file target_doc_u would contain all the listed items above that allows me to plot my point, as well as continue of the blog post in Mapbox to assign my filled extrusion and set height based on my value property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.