Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 please 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;

Thank you

share|improve this question
1  
This are actually two question: 1) project geoJSON layer, 2) fixing the bounds of your map. Is this correct? –  Stefan Dec 18 '12 at 10:17
    
Did you set the correct file type. Is it a json-file or a geojson-file? –  Stefan Dec 18 '12 at 10:44
    
yes, thank for editing –  SriRamaChandra.G Dec 19 '12 at 9:37
add comment

2 Answers

@Stefan Van Der Hoorn'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);

i hope it helps you...

share|improve this answer
    
You are right, that was a part I forgot. Thanks for the input :) –  Stefan Dec 18 '12 at 12:23
    
you're welcome :) –  Aragon Dec 18 '12 at 13:20
    
I tried it changing srid to 4326 with ogr2ogr but there is a problem in finding srid with GDAL libraries. However I could able to get a .json file finally with this error. If I would like to change the getsrid() to ST_SRID() where can I find it. ------------------------------ 'ERROR 1: ERROR: function getsrid(geometry) does not exist LINE 1: SELECT getsrid("geom") FROM (select * from public.roads) AS ... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.' –  SriRamaChandra.G Dec 19 '12 at 9:34
    
I done in this way : 'ogr2ogr -f "GeoJSON" neighborhoods.json PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" -sql "select gid, name, the_geom from neighborhoods"' –  SriRamaChandra.G Dec 19 '12 at 9:43
add comment

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()
                })
share|improve this answer
    
I tried but no use –  SriRamaChandra.G Dec 18 '12 at 10:21
    
Did you edit the projection to the correct projection? I don't know what projection you are working with. And do you see an error in FireBug? –  Stefan Dec 18 '12 at 10:24
    
projection is set to EPSG4326 at the conversion of json file. Initially it was default –  SriRamaChandra.G Dec 18 '12 at 10:29
    
Can you share the whole code? Did you add the layer in map.addLayers? –  Stefan Dec 18 '12 at 10:32
    
It is better if you edit your question. That makes it much easier to read –  Stefan Dec 18 '12 at 10:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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