简体   繁体   中英

How do I extrude a geojson polygon in harp Gl using purely Javascript?

we are experimenting with harp gl to replace a custom building tool for geojson. I want to visualize my polygons with the extruded polygon technique. Coming from MapBox GL I was able to have two properties on my geojson feature.properties called height and base height. So I have Some GeoJson that I'm using for testing. I cannot seem to get the extruded polygon to show up. I am writing this in Javascript, using the https://unpkg.com/@@here/harp.gl/dist/harp.js source. I am able to render points on my map. Just not these polygons. (ignore the walls for now, I want to see the floors first)

What I would like to achieve (This is how it looks in MapBox): 图片

function for reseting building data:

    resetBuildings: function (data) {
        const dataProvider = new harp.GeoJsonDataProvider("buildings", data);
        var geoJsonBuildingDataSource;
        if (this.mapView.getDataSourceByName("buildings") == null) {
            geoJsonBuildingDataSource = new harp.VectorTileDataSource({
                dataProvider,
                name: "buildings",
                styleSetName: "geojson",
            });
            this.mapView.addDataSource(geoJsonBuildingDataSource);
        }
        else { geoJsonBuildingDataSource = this.mapView.getDataSourceByName("buildings") }
        const theme = {
            styles: {
                geojson: this.getStyleSet()
            },
        };
        geoJsonBuildingDataSource.setTheme(theme);

My StyleSet definition(?):

    getStyleSet: function(){
        return [
            {
                when: ["==", ["geometry-type"], "Polygon"],
                technique: "extruded",
                renderOrder: 1000,
                constantHeight: true,
                color: "#FF0000",
                transparent: false,
                opacity: 0.8,
                lineWidth: 1,
                lineColor: "#003344",
                height: ["number", ["get", "base_height"], 10],
                floorHeight: ["number", ["get", "base_height"], 0]
            }
        ];
    }

My test GeoJson: {"type":"FeatureCollection","features":[{"id":"6223a3f0-2835-4ad8-8456-3ccbefc0d19c","type":"Feature","properties":{"level":1,"name":"base","height":0.2,"base_height":0,"color":"grey","message":"","coordinates":null},"geometry":{"type":"Polygon","coordinates":[[[174.81047388530345,-36.909499151794726],[174.81050337403076,-36.90965937993286],[174.8107788255581,-36.909634193592254],[174.8107567090126,-36.9094734295196],[174.81047388530345,-36.909499151794726]]]}},{"id":"a0583d91-f038-4d6e-8e32-99db806982bc","type":"Feature","properties":{"level":0,"name":"Wall","height":5,"base_height":0.2,"color":"blue","message":"","coordinates":null},"geometry":{"type":"LineString","coordinates":[[174.8104752256997,-36.909499687675165],[174.81075804940883,-36.90947396540121],[174.81078083615245,-36.90963526535126],[174.81050337403076,-36.909658844053574],[174.81049399125453,-36.909598825525144],[174.81050337403076,-36.909658844053574],[174.81078083615245,-36.90963526535126],[174.81075804940883,-36.90947396540121],[174.8104752256997,-36.909499687675165]]}},{"id":"ec219095-8891-440b-9fea-2db9cf74c7e0","type":"Feature","properties":{"level":0,"name":"Wall","height":5,"base_height":0.2,"color":"blue","message":"","coordinates":null},"geometry":{"type":"LineString","coordinates":[[174.8104752256997,-36.90950022355558],[174.81048594887454,-36.909558634520565],[174.8104752256997,-36.90950022355558]]}},{"id":"628a2754-c1b6-43a4-975b-eb45084d3853","type":"Feature","properties":{"level":1,"name":"base","height":0.2,"base_height":0,"color":"grey","message":"","coordinates":null},"geometry":{"type":"Polygon","coordinates":[[[174.8106367829821,-36.909648805405354],[174.81078191913713,-36.90963279956284],[174.81078129891483,-36.909646601064004],[174.81085540198825,-36.90961396183521],[174.81089173813234,-36.90967024159831],[174.81086324249378,-36.90968392941179],[174.8108926788683,-36.909721493301554],[174.8109264286474,-36.90971335389515],[174.81098518665493,-36.909814312925626],[174.81076392360183,-36.90990054860199],[174.81063578104482,-36.90969921056885],[174.8106354709525,-36.909651008673336],[174.8106367829821,-36.909648805405354]]]}},{"id":"d5a1c90d-295e-463b-9530-7643e411ca38","type":"Feature","properties":{"level":0,"name":"Wall","height":5,"base_height":0.2,"color":"blue","message":"","coordinates":null},"geometry":{"type":"LineString","coordinates":[[174.81063687126533,-36.909700434854784],[174.81076318373476,-36.90989889886059],[174.8109779359279,-36.90981381148965],[174.81076318373476,-36.90989889886059],[174.81063687126533,-36.909700434854784]]}}]} {"type":"FeatureCollection","features":[{"id":"6223a3f0-2835-4ad8-8456-3ccbefc0d19c","type":"Feature","properties":{"level":1,"name":"base","height":0.2,"base_height":0,"color":"grey","message":"","coordinates":null},"geometry":{"type":"Polygon","coordinates":[[[174.81047388530345,-36.909499151794726],[174.81050337403076,-36.90965937993286],[174.8107788255581,-36.909634193592254],[174.8107567090126,-36.9094734295196],[174.81047388530345,-36.909499151794726]]]}},{"id":"a0583d91-f038-4d6e-8e32-99db806982bc","type":"Feature","properties":{"level":0,"name":"Wall","height":5,"base_height":0.2,"color":"blue","message":"","coordinates":null},"geometry":{"type":"LineString","coordinates":[[174.8104752256997,-36.909499687675165],[174.81075804940883,-36.90947396540121],[174.81078083615245,-36.90963526535126],[174.81050337403076,-36.909658844053574],[174.81049399125453,-36.909598825525144],[174.81050337403076,-36.909658844053574],[174.81078083615245,-36.90963526535126],[174.81075804940883,-36.90947396540121],[174.8104752256997,-36.909499687675165]]}},{"id":"ec219095-8891-440b-9fea-2db9cf74c7e0","type":"Feature","properties":{"level":0,"name":"Wall","height":5,"base_height":0.2,"color":"blue","message":"","coordinates":null},"geometry":{"type":"LineString","coordinates":[[174.8104752256997,-36.90950022355558],[174.81048594887454,-36.909558634520565],[174.8104752256997,-36.90950022355558]]}},{"id":"628a2754-c1b6-43a4-975b-eb45084d3853","type":"Feature","properties":{"level":1,"name":"base","height":0.2,"base_height":0,"color":"grey","message":"","coordinates":null},"geometry":{"type":"Polygon","coordinates":[[[174.8106367829821,-36.909648805405354],[174.81078191913713,-36.90963279956284],[174.81078129891483,-36.909646601064004],[174.81085540198825,-36.90961396183521],[174.81089173813234,-36.90967024159831],[174.81086324249378,-36.90968392941179],[174.8108926788683,-36.909721493301554],[174.8109264286474,-36.90971335389515],[174.81098518665493,-36.909814312925626],[174.81076392360183,-36.90990054860199],[174.81063578104482,-36.90969921056885],[174.8106354709525,-36.909651008673336],[174.8106367829821,-36.909648805405354]]]}},{"id":"d5a1c90d-295e-463b-9530-7643e411ca38","type":"Feature","properties":{"level":0,"name":"Wall","height":5,"base_height":0.2,"color":"blue","message":"","coordinates":null},"geometry":{"type":"LineString","coordinates":[[174.81063687126533,-36.909700434854784],[174.81076318373476,-36.90989889886059],[174.8109779359279,-36.90981381148965],[174.81076318373476,-36.90989889886059],[174.81063687126533,-36.909700434854784]]}}]}

Thank you in advance!

I figured it out. I was using the wrong kind of data source. I needed to use a FeaturesDataSource. My style set definition was also wrong, and I was not setting it to the map theme.

How I now initialize map:

        this.styleSet = this.getStyleSet();
        hereMapsHelper.geoJsonObj = JSON.parse(GeoJson);
        const canvas = document.getElementById(mapContainerId);
        var options = {
            canvas,
            theme: {
                extends: "https://unpkg.com/@here/harp-map-theme@latest/resources/berlin_tilezen_base.json",
                styles: {
                    geojson: this.styleSet,
                }
            }
        }
        var map = new harp.MapView(options);

function for buildings:

    resetBuildings: function (data) {
        var geoJsonBuildingDataSource;
        if (this.mapView.getDataSourceByName("buildings") != null) {
            var existingBuildingDataSource = this.mapView.getDataSourceByName("buildings")
            this.mapView.removeDataSource(existingBuildingDataSource);
        }
        geoJsonBuildingDataSource = new harp.FeaturesDataSource({
            geojson: data,
            name: "buildings",
            styleSetName: "geojson",
            maxGeometryHeight: 30000
        });
        this.mapView.addDataSource(geoJsonBuildingDataSource);
    },

Function to define style set:

    getStyleSet: function () {
        const color = new THREE.Color("blue");
        const colorString = "#" + color.getHexString();
        return [
            {
                description: "geoJson property-based style",
                when: ["==", ["geometry-type"], "Polygon"],
                technique: "extruded-polygon",
                renderOrder: 1000,
                height: ["number", ["get", "base_height"], 10],
                floorHeight: ["number", ["get", "base_height"], 0],
                attr: {
                    color: colorString,
                    transparent: true,
                    opacity: 0.8,
                    boundaryWalls: false,
                    constantHeight: true,
                    lineWidth: 1,
                    lineColor: "#003344",
                    emissive: colorString,
                    emissiveIntensity: 0.45
                },
            },
            {
                description: "geoJson property-based style",
                when: ["==", ["geometry-type"], "Point"],
                technique: "circles",
                renderOrder: 2000,
                color: "#00FF00",
                size: 15,
            }
        ];
    },

This only renders floors, as I havent defined the style for the walls yet. But its progress!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM