Getrennte Layer aus geoJSON Featurecollection erzeugen

geoJSON mit FeatureCollection:


{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"color": "#FFFF00"},"geometry":{"type":"Polygon","coordinates":[[[13.393707275390625,52.512669003368266],[13.410186767578123,52.5300077193577],[13.388900756835938,52.53836128351202],[13.367958068847656,52.53126085529294],[13.370704650878906,52.517474393230245],[13.380317687988281,52.51663871100423],[13.380317687988281,52.508072051744215],[13.385124206542969,52.51287794429001],[13.393707275390625,52.512669003368266]]]}},{"type":"Feature","properties":{"color": "#00FF00"},"geometry":{"type":"Polygon","coordinates":[[[13.394737243652344,52.54587813253921],[13.374481201171875,52.54483420270007],[13.345298767089844,52.528754547664185],[13.352508544921875,52.510579539510864],[13.381004333496094,52.50305664701985],[13.408470153808594,52.50640031375411],[13.421859741210936,52.518727886767266],[13.42529296875,52.53355817830341],[13.406753540039062,52.54253746965676],[13.394737243652344,52.54587813253921]]]}}]}

Zeichnen im Client:

geojson_layer = new OpenLayers.Layer.Vector("GeoJSON", {
		projection : projLonLat,
		strategies : [ new OpenLayers.Strategy.Fixed() ],
		protocol : new OpenLayers.Protocol.HTTP({
			url : "polygons.geojson",
			format : new OpenLayers.Format.GeoJSON()
		}),
		styleMap : stylemap.styles["yellow"]
	});
	
	map.addLayer(geojson_layer);

Folgender Code funktioniert wunderbar. Die geoJSON Datei wird eingelesen und gezeichnet. Natürlich alles mit der stylemap → yellow.

Was ich möchte:

  • Anhand des Arguments “color” das ich unter properties beim feature mitgebe (siehe json-string) die Farbe für jedes einzelne Polygon setzen. Oder eine Alternativlösung mit dem gleichen Effekt (Setzen der Farbe im Backend / geoJSON).

  • Ein neuer Layer für jedes Feature bzw Polygon (Erstmal nebensächlich)

Danke im Voraus!

Habe für das Style-Problem eine Lösung gefunden:

	// Style fuer Polygon
	var polygon_style = {
		
			fillColor : '${color}',
			strokeColor : '${color}',
			strokeOpacity : "0.9",
			fillOpacity : "0.2"
	};
	
	geojson_layer = new OpenLayers.Layer.Vector("GeoJSON", {
		projection : projLonLat,
		strategies : [ new OpenLayers.Strategy.Fixed() ],
		protocol : new OpenLayers.Protocol.HTTP({
			url : url_geoJSON_Polygon,
			format : new OpenLayers.Format.GeoJSON()
		}),
		styleMap :new OpenLayers.StyleMap(polygon_style)
	});
	
	map.addLayer(geojson_layer);
{
      "type": "Feature",
      "properties": {"color": "#2EFE2E"},
      "geometry": {
        "type": "Point",
        "coordinates": [
          13.41379165649414,
          52.54431222846979
        ]
      }
    }

Aus den features getrennte Layer zu erzeugen ist eher nebensächlich. Falls es eine elegante Lösung gibt bin ich natürlich für jeden Ansatz offen :slight_smile: