You are not logged in.

Announcement

*** NOTICE: forum.openstreetmap.org is being retired. Please request a category for your community in the new ones as soon as possible using this process, which will allow you to propose your community moderators.
Please create new topics on the new site at community.openstreetmap.org. We expect the migration of data will take a few weeks, you can follow its progress here.***

#1 2013-09-27 13:57:33

Markovicho
Member
Registered: 2013-09-23
Posts: 10

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!

Last edited by Markovicho (2013-09-30 08:50:05)

Offline

#2 2013-09-30 10:21:03

Markovicho
Member
Registered: 2013-09-23
Posts: 10

Re: Getrennte Layer aus geoJSON Featurecollection erzeugen

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 :-)

Offline

Board footer

Powered by FluxBB