I have a Django model which contains the coordinates for a polygon:
geometria = models.PolygonField(srid=4326, null=True, geography=True)
I manually took the lat-long coordinates for a polygon from the Google maps web interface and entered them into the database. I believe the gps coordinates Google maps provide are 4326.
I plotted these coordinates on google maps using @react-google-maps/api and it matches what I expected. (See below image)
Next I tried to replicate the polygon shape using Postgis, and create an SVG of it, but this is where I had problems. As you can see below the green polygon has the same basic shape, but has been stretched horizontally when compared to the output from google maps.
I believe the problem is that the svg is using the 4326 projection, instead of 3857 which is used by google maps? I have tried to transform the data in postgis, but nothing I do seems to make any difference. Note that when I create the svg without the ST_Transform, it is the same shape as below. Any ideas how I can solve this?
I am using the following SQL in Django to pull the data from the database:
cursor.execute("SELECT ST_AsSVG(ST_AsEWKT(ST_Transform(ST_AsEWKT(geometria),3857))) FROM public.locations_locationdata WHERE id= %s", [self.id])
Where geometria is the database field which contains the polygon.
I use the following python to build the svg file:
svg_string = []
svg_string.append('<svg viewBox="')
svg_string.append(calculated_view_box)
svg_string.append('" xmlns="http://www.w3.org/2000/svg" width="512" height="512" version="1.1" transform="scale(1 -1) rotate(90)" transform-origin="256 256">')
svg_string.append('<path fill="green" stroke="blue" stroke-width="1" d="')
svg_string.append(reconstruct_path_string)
svg_string.append('"/></svg>')
<svg viewBox="5735636.0 75092.0 419.0 619.0" xmlns="http://www.w3.org/2000/svg" width="512" height="512" version="1.1" transform="scale(1 -1) rotate(90)" transform-origin="256 256"><path fill="green" stroke="blue" stroke-width="1" d="M 5736044.89547394 75658.7303669334 L 5735771.38348506 75102.09404005 5735654.72065871 75164.2146256582 5735646.59433588 75700.4781146285 5735842.07136171 75663.4061145318 Z"/></svg>
Any ideas how I can fix the svg so that it matches the google maps shapes?