[gdal-dev] Reg: Getting center coordinates for the attributes in a geojson

Even Rouault even.rouault at spatialys.com
Wed Feb 18 03:29:52 PST 2015


Le mercredi 18 février 2015 12:20:19, Manikanta Kondeti a écrit :
> That looks pretty awesome. It is giving me a geometry object with
> centroidx, centroidy.
> 
> But the issue is I lost all the 'coordinates' array in the geometry object.
> 
> So what I need is both coordinates and centroid as well.

GeoJSON only supports one geometry field per object. The closest you can do is 
to have your original object and the object with centroid.

You can do that into several steps:
- reproject with ogr2ogr your shape into another shape
- with a variation of my previous proposed ogr2ogr with st_centroid, compute 
centroid and reproject into another shape
- create a union VRT as we have discussed recently in
http://lists.osgeo.org/pipermail/gdal-dev/2015-February/040955.html
- convert the union VRT into GeoJSON

~~~~~~

Hum, there's perhaps another simpler solution if you are OK for having the 
centroid coordinates as regular GeoJSON attributes. You can try something like 
:
"select st_x(st_centroid(geometry)) as centroid_x, st_y(st_centroid(geometry)) 
as centroid_y, * from the_shape_layer"

Note: reprojection of the centroid coordinates will not occur with -t_srs. You 
should do it manually with st_transform(). Or just reproject your shapefile 
before.

> 
> Thanks,
> Mani
> 
> On Wed, Feb 18, 2015 at 4:40 PM, Even Rouault <even.rouault at spatialys.com>
> 
> wrote:
> > Le mercredi 18 février 2015 12:02:23, Manikanta Kondeti a écrit :
> > > Hello,
> > > 
> > > Thanks for the response.  Can I put a flag for the centroid
> > > computation?
> > > 
> > > For eg:  ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp
> > > 
> > >  (--flag for centroid??)
> > 
> > If your GDAL build is configured with spatialite support (and is at least
> > GDAL
> > 1.10), you can try the ST_Centroid() SQL function with the SQLite dialect
> > ( http://www.gdal.org/ogr_sql_sqlite.html )
> > 
> > Something like :
> > 
> > ogr2ogr -f geojson out.json poly.shp \
> > 
> >  -sql "select st_centroid(geometry) as geometry, * from poly" -dialect
> > 
> > sqlite
> > 
> > > On Wed, Feb 18, 2015 at 4:20 PM, Even Rouault <
> > 
> > even.rouault at spatialys.com>
> > 
> > > wrote:
> > > > Le mercredi 18 février 2015 11:43:10, Manikanta Kondeti a écrit :
> > > > > Hi all,
> > > > > 
> > > > > I'm working on a project which aims  to visualize Spatial Vector
> > > > > Data on
> > > > 
> > > > a
> > > > 
> > > > > browser. Using gdal I will convert Shapefiles, kml, gml into
> > > > > geojson and send it back to the browser.
> > > > > 
> > > > > I want to draw labels(attributes) on the map, using GDAL can I get
> > 
> > each
> > 
> > > > > feature's centre coordinates without computing them on the browser
> > > > > side,
> > > > 
> > > > so
> > > > 
> > > > > that I can easily draw them  on the map?
> > > > 
> > > > Mani,
> > > > 
> > > > OGR_G_Centroid() /  OGRGeometry::Centroid() should help.
> > > > 
> > > > See
> > > > http://www.gdal.org/ogr__api_8h.html#a23f5a19a81628af7f9cc59a37378cb2
> > > > b
> > > > 
> > > > Even
> > > > 
> > > > --
> > > > Spatialys - Geospatial professional services
> > > > http://www.spatialys.com
> > 
> > --
> > Spatialys - Geospatial professional services
> > http://www.spatialys.com

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list