3

I'm reading POLYGON WKT strings using OpenLayers.Format.WKT().read(). The polygons use 0,360 longitudes but my map is -180,180. When I display the polygons on the map they are displayed outside of the map. How can I display them on their correct location? I tried OpenLayers transforms and Proj4js transforms (plus defining the 0,360 CRS using +lon_wrap=180) but nothing seems to work! Help would be appreciated ;-)

1

I made a quick and dirty solution. The below function only worked when I added the first line: wktstring = wkt.read(wktstring).geometry.toString();

function wktto180(wktstring)
{
wktstring = wkt.read(wktstring).geometry.toString();
var newwktstring = "POLYGON((";
wktstring = wktstring.substring(9,wktstring.length - 2);
coords = wktstring.split(",");
for(var i = 0; i < coords.length; i++)
    {
    coord = coords[i].split(" ");
    if(coord[0] > 180)
        {
        newwktstring = newwktstring + (coord[0] - 360) + " " + coord[1] + ","
        }
    else
        {
        newwktstring = newwktstring + coord[0] + " " + coord[1] + ","
        }
    }
newwktstring = newwktstring.substring(0,newwktstring.length - 1) + "))";
return newwktstring;
}
  • Ordinarily this will completely screw up any polygons crossing the cut at 0/360 or +180/-180. – whuber Sep 17 '13 at 2:10
1

You can correct it before making feature, after making feature or before adding it to the layer.

it think it would be easiest to do it after making feature i.e something like (it is not actual code)

feature=wkt.read();
foreach(feature){
    points = feature.geometry.components.components
    points.foreach(
        if(feature.geometry.lon >=180)
            feature.geometry.components.components.lon -= 360;
    );
}
layer.addFeatures(feature);
  • The features I'm working with are single polygons. I assume with foreach the polygon points are accessed one by one? I'm not sure if I fully understand your code exanple ;-) – joosthoek Aug 16 '13 at 14:36
  • you can see the structure of the polygon object in console or in debugger. In my case see the update. – neogeomat Aug 16 '13 at 16:28
  • Could you please write actual code because I can't get your code to work after debugging. – joosthoek Aug 20 '13 at 10:03
  • can you give an example wkt string to work with? – neogeomat Aug 20 '13 at 14:37
1

I don't think defining a new proj4js item is a good idea in such cases. Assuming the latitudes of all geometries are correct, i.e. between (-90, 90). You can move the features' geometry before adding them to the layer, in such case you don't need to change each point's coordinates.

var wktParser = new OpenLayers.Format.WKT();
var features = wktParser.read(...);

for (var i=0; i<features.length; i++) {
    features[i].geometry.move(-180, 0); // assuming the 0 value coord from wkt is the same as the -180 in your map
}

vector_layer.addFeatures(features);

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.