10

I have a CSV file where one line looks like this:

Duration, user, lat-start, long-start, lat-end, long-end
298, Casual,38.9101,-77.0444,38.91554,-77.03818

Each row in the CSV has two points and I need to draw a line between those two points for each row.

How can I do this using QGIS 1.8.0?

I have tried using the delimited text file plug-in, but it just draws points and has no line option from what I can see.

0

3 Answers 3

9

With a bit of text processing (maybe in Excel), you can bring your data into this form:

Duration;user;wkt
298;Casual;LINESTRING(-77.0444 38.9101,-77.03818 38.91554)

which can be interpreted by QGIS as well known text (WKT) using Add delimited text layer.

Note that you need blanks and comma as delimiters for lon&lat values and linestring members, and lon values have to be first. In the dialogue, only check semicolon as column delimiter.

Applying EPSG:4326 as CRS , you end up at New Hampshire Avenue Nordwest in Washington DC.


EDIT

this one works from your second example:

seconds-total;Subscription-Type;gis-data
298;Casual;LINESTRING(-77.0444 38.9101,-77.03818 38.91554)
27926;Casual;LINESTRING(-77.0444 38.9101,-77.03818 38.91554)
214;Casual;LINESTRING(-77.0511 38.8573,-77.051516 38.848441)
960;Subscriber;LINESTRING(-77.024649 38.936043,-77.012289 38.915417)
378;Subscriber;LINESTRING(-77.04062 38.917761,-77.0471 38.9054)
2085;Subscriber;LINESTRING(-77.041606 38.904742,-76.9862 38.8803)
441;Subscriber;LINESTRING(-77.094875 38.88786,-77.10108 38.884616)
456;Subscriber;LINESTRING(-77.023086 38.8997,-77.022155 38.912719)
7
  • I started with this strategy, thanks so much for the help! I was able to change it to this format in Excel easily. Now, waiting for it to load. It's been the beach ball of death for about 10 mins which makes me think I may have too much data. How much data usually works? Do you think it will eventually show if I wait it out or should I restart? My csv has 365,000 rows or something.
    – lindzcook
    Commented Jun 28, 2013 at 17:08
  • I have not yet worked with such huge files, but that amount would better be put into a postgis database. Switching off rendering could improve speed.
    – AndreJ
    Commented Jun 28, 2013 at 19:23
  • I tried it with a small amount of data to see if the size was the problem. When I try layer, add delimited text layer with the information below, it tells me it can not add the layer because it is not valid. What am I doing wrong here?
    – lindzcook
    Commented Jun 28, 2013 at 19:46
  • seconds-total Subscription Type gis-data 298 Casual LINESTRING(-77.0444 38.9101,-77.03818 38.91554) 27926 Casual LINESTRING(-77.0444 38.9101,-77.03818 38.91554) 214 Casual LINESTRING(-77.0511 38.8573,-77.051516 38.848441) 960 Subscriber LINESTRING(-77.024649 38.936043,-77.012289 38.915417) 378 Subscriber LINESTRING(-77.04062 38.917761,-77.0471 38.9054) 2085 Subscriber LINESTRING(-77.041606 38.904742,-76.9862 38.8803) 441 Subscriber LINESTRING(-77.094875 38.88786,-77.10108 38.884616) 456 Subscriber LINESTRING(-77.023086 38.8997,-77.022155 38.912719)
    – lindzcook
    Commented Jun 28, 2013 at 19:46
  • 1
    Using blanks as delimiters is not a good idea, as lon and lat have to be separated by that (and blanks in column names may also lead to trouble). See my extended answer.
    – AndreJ
    Commented Jun 29, 2013 at 4:18
3

As an alternative to Andre's very workable and straight-forward solution, you could use either the 'Points To One'or "Points To Path" plugins (install by going Plugins->Fetch Python Plugins). You have the user ID which can be your line ID. You will need to separate out your start and end points to make sure they are separate points (as it appears your data for each two-point line is in one row).

2

For the record. If you have updated to a newer QGIS the most dynamic solution would be to use a virtual layer in QGIS. It is a virtual table that would get automatic updated, when ever changes to the CSV file is made on disk. You need to open the CSV with the Data Source Manager | Delimited Text and then make a virtual layer like:

select
  make_line(make_point("long-start","lat-start"),make_point("long-end","lat-end")) as geometry
from
  my_csv_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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