How to convert FeatureCollection to GeometryCollection or MultiPolygon? - postgresql

I have many polygons that need to be drawn manually and then get geo-coordinates.
I need to get the coordinates of the drawn polygons in GeoJSON format.
In this format:
"{"type":"MultiPolygon","coordinates":[[[[37.4653933,55.3959159]...}"
"{"type":"Polygon","coordinates":[[[37.475738525390625,55.41420507450017]...}"
Or in this:
"{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[-98.0419921875,39.027718840211605]...}]}"
I draw polygons at http://geojson.io/.
But from this site I can only get data in the format with the FeatureCollection type.
I found another site - https://rodic.fr/blog/online-conversion-between-geometric-formats/, on which I can convert to GeoJSON format, but this site can only convert type GeometryCollection.
I cannot find how to convert FeatureCollection to GeometryCollection or MultiPolygon or to Polygon.
How to solve?
Many thx!

To get the coordinates in the geojson format, you can use the following snippet:
WITH geojson_featurecollection AS (
SELECT ''::json AS fc
)
SELECT (json_array_elements(fc->'features'))->>'geometry'
FROM geojson_featurecollection;
in which you paste your entire FeatureCollection definition (coming from the http://geojson.io website after your edits) inside of the quotes

Related

How to convert EPSG:28992 to EPSG:4326?

I have a list of coordinates which i need to convert to longitude and latitude in the folowing way:
What are the math transformations and formulas used to achieve this?
QGis might also be helpful to convert between several projections.
Once you can see your geometry as layer in Qgis, right click on the layer and select
Export => Save Features As ... and select the wanted format.
https://www.qgis.org/en/site/forusers/download.html

SVG generation using ST_AsSVG - Transform (EPSG: 4326) to (EPSG: 3857)

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?
The problem was that I incorrectly transposed the lat and lng values when creating the GEOSGeometry object. I later fixed the transposition when passing the the lat and lng back to Google maps, which hid the problem.
See below:
#polygon = GEOSGeometry('POLYGON ((51.527768 -0.679638,51.525311 -0.674638,51.524263 -0.675196,51.524190 -0.680013,51.525946 -0.679680,51.527768 -0.679638))', srid=4326)
polygon = GEOSGeometry('POLYGON ((-0.679638 51.527768, -0.674638 51.525311, -0.675196 51.524263, -0.680013 51.524190, -0.679680 51.525946, -0.679638 51.527768))', srid=4326)
Coordinates should be (longitude latitude) pairs

Do I have to use geojson files to get data when I click polygons on the map in mapbox gl js?

I have 16,000 rows of data in excel.
In this excel, I have census block numbers.
What I want to achieve is that I want to make a map layer for these block numbers.
Then, if I click a polygon, the information of a block number display on the popups, like which state, county, and so on.
How can I do that? Do I have to use geojson files for this?
Because, many exmaples in the mapbox use geojson.
But I don't have any coordinates for these block numbers and 16,000 rows are really big to me. I got only block numbers.
But I got shapefiles for whole state block numbers.It is too big to upload mapbox gl js.
If geojson file is the way to achieve my goal, can someone tell me the orders that I can follow?
Or is there other way, please advise me.
Thanks.
So you have a geometry file ("shapefiles for whole state block numbers") and a data file ("16,000 rows of data in excel"). You want the user to click on a boundary in the geometry, and see data from the data file. You have two main options:
Join the data and geometry first (eg, use geojson-join, then upload to Mapbox as a tileset.
Upload just the geometry. Load the CSV file when the web page loads, then when the user clicks, display something from the CSV file.

Put points on the map, longitude/latitude not where they should be although SRID the same

Thank you for looking at this - I'm sure it's a simple question but this is all very new to me and after a lot of internet searching my points are still in the sea!
I have a set of UK postcode data that I have converted into latitude and longitude. I also have a UK counties shape file.
The code that I have used to convert the latitude/longitude to a geometry point column is as follows:
ALTER TABLE colic ADD COLUMN longlat geometry(POINT,27700);
update colic set longlat = st_setsrid(st_point( longitude, latitude), 27700);
And this produces a column that has values as follows (I've only included a couple!):
"0101000020346C000048BF7D1D3867E83FC05B2041F1334A40"
"0101000020346C0000F085C954C1A8F7BFA1F831E6AE954A40"
"0101000020346C000020D26F5F07CEF4BFE3361AC05B504A40"
"0101000020346C00007D3F355EBA49F73FB6847CD0B3614A40"
The .prj from my shape file is:
PROJCS["British_National_Grid", GEOGCS["GCS_OSGB_1936",
DATUM["D_OSGB_1936", SPHEROID["Airy_1830",6377563.396,299.3249646]],
PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",400000.0],
PARAMETER["False_Northing",-100000.0],
PARAMETER["Central_Meridian",-2.0],
PARAMETER["Scale_Factor",0.9996012717],
PARAMETER["Latitude_Of_Origin",49.0], UNIT["Meter",1.0]]
And I set its SRID to 27700 when I upload it using the PostGIS shapefile uploader.
In PG Admin, when I ask:
select st_srid(geom) from counties limit 1;
select st_srid(longlat) from colic limit 1;
I get 27700 for both.
But...when I try any spatial query I get no response, and when I load the layers into quantum, the point data are off the tip of Cornwall in the north Atlantic, and only represented as one dot on the scale of the counties map.If I zoom in on them, they are 'there', but just not in east anglia where they should be!
This is my first attempt at both SQL and mapping - I'm sure there is something really simple that I have missed.
You are mixing up spatial references (SRS). The SRS in the .prj file (aka SRID=27700) is projected eastings and northings, which have units of metres. This is not latitude and longitude! Furthermore, it is a deception to call a column longlat when it isn't for longitude/latitude coordinates.
If you try to insert latitude/longitude in a column with SRID=27700, the points will not behave as expected, e.g., they will be in the far bottom left corner of a map.
If you have lat/long data from WGS84 (SRID=4326), you can transform this to eastings and northings:
UPDATE colic SET
geom = ST_Transform(ST_SetSRID(ST_Point(longitude, latitude), 4326), 27700);

Multiple maker on the same place with leaflet

I tried to display a lot of marker on a leaflet map. The data of the marker - coordinates, text... - are stored in a text file.
I made a parser who parse the text, put the data in a big array and then i loop on this array to display each marker.
My problem here is that i got many markers - more than 200, and many of them got the same coordinates.
My code loops well but display only the last marker with each coordinates, how can i display them all properly?
After digging the web around i find a solution which seems to pretty fit my issue.
Leaflet allows you to create "markercluster" which can gather marker together.
Here is a live example : http://leaflet.github.io/Leaflet.markercluster/example/marker-clustering-realworld.388.html