1

I have a json file well formed. I want to read a json file as a base layer from OpenLayers.

    <script type="text/javascript">
        var styles = new OpenLayers.StyleMap({
                "default": {
                    strokeWidth: 2
                },
                "select": {
                    strokeColor: "blue",
                    strokeWidth: 4
                }
            });
        
            // add rules from the above lookup table
            styles.addUniqueValueRules("default", "RP_TYPE", {
                10: {strokeColor: "#000000", strokeWidth: 2},
                12: {strokeColor: "#222222", strokeWidth: 2},
                14: {strokeColor: "#444444", strokeWidth: 2},
                16: {strokeColor: "#666666", strokeWidth: 2},
                18: {strokeColor: "#888888", strokeWidth: 2},
                19: {strokeColor: "#666666", strokeWidth: 1}
            });

        
            // add rules from the above lookup table
            styles.addUniqueValueRules("default", "RP_TYPE", {
                10: {strokeColor: "#000000", strokeWidth: 2},
                12: {strokeColor: "#222222", strokeWidth: 2},
                14: {strokeColor: "#444444", strokeWidth: 2},
                16: {strokeColor: "#666666", strokeWidth: 2},
                18: {strokeColor: "#888888", strokeWidth: 2},
                19: {strokeColor: "#666666", strokeWidth: 1}
            });

            var vectors = new OpenLayers.Layer.Vector("Lines", {
                strategies: [new OpenLayers.Strategy.Fixed()],                
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "data/locality_region.json",
                    format: new OpenLayers.Format.GeoJSON()
                }),
                styleMap: styles
            });
        
            map.addLayer(vectors);
            map.addControl(new OpenLayers.Control.LayerSwitcher());
            map.zoomToMaxExtent();

        }  

I have another json files which are downloaded from api and those files works fine. How about my own json file that got converted from db.

Do I need to work more on it?

If so, can you guide me how to project a json on OpenLayers that created from a PostgreSQL db?

How can I fix the bounds for my own map or layer?

2
  • 1
    This are actually two question: 1) project geoJSON layer, 2) fixing the bounds of your map. Is this correct?
    – Stefan
    Dec 18, 2012 at 10:17
  • Did you set the correct file type. Is it a json-file or a geojson-file?
    – Stefan
    Dec 18, 2012 at 10:44

2 Answers 2

4

@StefanVanDerHoorn's code is true for converting json file projection but in projection you firstly have to define your proj.

var epsg4326 = new OpenLayers.Projection("EPSG:4326");

if this code does not work, you can transform it before adding your vector.

vectors.transform(map.getProjectionObject(), epsg4326 );
map.addLayers([vectors]);

or in PostGIS you can transform it:

UPDATE myTable SET  the_geom = ST_Transform(the_geom,4326);    
SELECT UpdateGeometrySRID('myTable', 'the_geom', 4326);
0
2

You can set your projection in your GeoJSON layer. Maybe this helps.

   var vectors = new OpenLayers.Layer.Vector("Lines", {
                projection: epsg4326, 
                strategies: [new OpenLayers.Strategy.Fixed()],                
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "data/locality_region.json",
                    format: new OpenLayers.Format.GeoJSON()
                })
0

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