-3

I have lat/longs of a particular area. How do I insert multiple lat/lons corresponding to a single area in CSV file? I am just using random numbers right now in my CSV file. I have attached a screenshot to show my CSV file. I don't want single latitude and longitude but multiple corresponding to a single area. I have to plot them later on map box

This is the code I am using to convert my csv file to geojson file

library(geojsonio)
library(rgdal)
file_to_geojson(input = "FINAL DATA ENTRY.csv",method ="web" ) 

geojson file when created gives coordinates as this. If you see red highlighted area in my geojson output file I converted from a csv file, What I want is geometry should be polygon or multipolygon and it should include 100 coordinates not single one. How do I provide those 100+ coordinates to csv file? Ill be more clear if someone is not able to understand me. Let me know. Ignore my bad English, please.enter image description here

Added My csv file.

Daman deep
  • 631
  • 3
  • 14
  • 4
    I don't like it either when people downvote without communicating the reason. People here like [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) examples. Only few have the solution off the top of their heads. They need to experiment a little. So pictures are not helpful. – Humpelstielzchen Jun 27 '19 at 06:34
  • @patL I am using csv and converting it to geojson file. But I want it be coverted as polygon yes. – Daman deep Jun 27 '19 at 06:39
  • @Humpelstielzchen I don't understand how do I make reproducible examples. I read the link but too much is written there. I still don't get it. – Daman deep Jun 27 '19 at 06:40
  • @patL That link I added because It has many lat/lons if you see not single coordinate. Just to show that I want my geojson like that. – Daman deep Jun 27 '19 at 06:47
  • @patL I added using dput but nothing renders why? – Daman deep Jun 27 '19 at 06:48
  • @Humpelstielzchen I have added code and shared files. That will now reproduce same code as I did. This is what you meant ? – Daman deep Jun 27 '19 at 06:55
  • please type `dput(head(your_data, 20))` and copy the output into your question. People on this site also don't like downloading files. don't know what you mean by 'not render' for dput... its just text copied from your console into your question – morgan121 Jun 27 '19 at 06:59
  • @RAB your_data is file name in dput command? – Daman deep Jun 27 '19 at 07:00
  • no...its the name of your dataframe – morgan121 Jun 27 '19 at 07:00
  • It can not be emphasized enough that a message is putting yourself at the mercy of strangers. If someone has the wit and knowledge to answer your question, they probably have other things they would like to do. Making your message clear, concise and user-friendly gives you the best hope of at least one of those strangers diverting their attention away from their life towards your problem. *source: the R inforno, ch 9.8* – Wimpel Jun 27 '19 at 07:03
  • @RAB if I have to add my csv file here what should be a correct command. dput(head("FINAL DATA ENTRY.csv", 20)) ? – Daman deep Jun 27 '19 at 07:05
  • what? in R, what is the name of your data?... – morgan121 Jun 27 '19 at 07:06
  • @Wimpel sir I tried my best to explain my question and this best is not enough understandable for someone else. How do I know what other is expecting If they don't clear it to me? – Daman deep Jun 27 '19 at 07:07
  • @RAB I didn't assign any variable to it. I just make it read csv file of mine . Those three lines I pasted is what I wrote in R – Daman deep Jun 27 '19 at 07:08
  • @Wimpel Now I understand how should I make clear it to people by means of things others provided me. Thanks to them. – Daman deep Jun 27 '19 at 07:09
  • try assigning it to a variable, then using that name – morgan121 Jun 27 '19 at 07:09
  • @RAB ```library(geojsonio) library(rgdal) dfentry<- read.csv("FINAL DATA ENTRY.csv") file_to_geojson(input="dfentry",method="web")``` It gives me error – Daman deep Jun 27 '19 at 07:12
  • you need to go back and learn R basics... – morgan121 Jun 27 '19 at 23:17

1 Answers1

1

Try this:

Using the sf-package which allows to combine points of a group (such as a state) to MULTIPOINT or POLYGON. In your file, some states only have one point so I just can convert it to MULTIPOINT.

And then save the sf-class as geojson.


library(geojsonio)
library(sf)
library(tidyverse)

data<- data[,c("State", "lat", "lon")]

sf_data <- st_as_sf(data, coords = c("lon", "lat"))

sf_data  %>%
  group_by(State) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON") -> res_sfdata


geojson_write(res_sfdata, file = "yourpath/res_sfdata.geojson")

You'll have to remove the other columns however. Because you have different information in them on each point you want to aggregate.

EDIT: In case you want to group by more columns:


data<- data[,c("State", "PC_Name", "lat", "lon")]

sf_data <- st_as_sf(data, coords = c("lon", "lat"))

sf_data  %>%
  group_by(State, PC_Name) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON") %>%
  group_by(State) %>%
  summarise(geometry = st_combine(geometry)) 
  st_cast("MULTIPOLYGON") -> res_sfdata
Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34
  • Can you tell me what to read [here](https://cran.r-project.org/web/packages/sf/sf.pdf).so that I can covert it to multipolygon? That's my first step right now, but I don't have any clue about this package. – Daman deep Jun 27 '19 at 07:20
  • You just have to use the code I provided. It should work on your csv. – Humpelstielzchen Jun 27 '19 at 07:26
  • How will it take coordinates. I have to paste those coordinates somwhere. – Daman deep Jun 27 '19 at 07:27
  • If you see [here](https://raw.githubusercontent.com/veenivinta/files/master/2014-India.geojson) I have lat longs available. But I have added more parameters this year. This geojson file was created last year . – Daman deep Jun 27 '19 at 07:29
  • When you get `res_sfdata` you'll just have a data.frame with every state and it's according polygon. If there is additionall information you have for every state, then you have to join it to `res_sfdata` and save it afterwards. – Humpelstielzchen Jun 27 '19 at 07:37
  • One state has multiple constituencies and each constituency has around 100+ coordinates – Daman deep Jun 27 '19 at 07:38
  • I have no idea, man. I just worked with your csv. And in there each state as several rows, all different, with a coordinate for each. I just aggregated the coordinates for each state. I don't know what information you want to attach to each state. – Humpelstielzchen Jun 27 '19 at 07:41
  • Sir. take for example first state Andaman and Nicobar its constituency name is Andaman and Nicobar. In the end you ll see two columns lat and lon. I don't want 1,2 there. I want [these](http://prntscr.com/o7au6d)there .See first row – Daman deep Jun 27 '19 at 07:46
  • This [link](https://raw.githubusercontent.com/veenivinta/files/master/2014-India.geojson) Find "Andaman & Nicobar Islands" above that are longs and lat of this constituency. How do I fit all those coordinates with 1,2. I have takes 1,2 just example in csv file. – Daman deep Jun 27 '19 at 07:49
  • I am going for lunch I ll back in an hour. Please stick with me here. – Daman deep Jun 27 '19 at 07:50
  • I cannot reproduce the geojson in the link with your csv. Each constituency in the csv has only one coordinate. If you have more coordinates per constituency, I'll add an update – Humpelstielzchen Jun 27 '19 at 08:05
  • Yes sir that is my point I have multiple points per constituency. – Daman deep Jun 27 '19 at 09:16