2

This question already has an answer here:

I know this question has been asked before in one form or another, but those answers didn't solve my issue.

I'm using QGIS 2.14.0 and trying to upload lat/lon coordinates from a csv file. When I drag the file into the QGIS window it loads up the attribute table perfectly, but will not show any of my points. When I try to upload the csv file manually (layer->add delimited text layer etc) and check off the correct stuff (csv, first record has field names, point coordinates) it says there's no data in the file while simultaneously combining different cells together; for example, it keeps xlat as a heading, but then combines my ylon with the location name for the first point. So like I said, dragging and dropping it into QGIS loads up the data correctly on the attribute table, but still won't show my points. I'm completely at a loss.

An important note: when I load the csv file into ArcGIS, the points come up perfectly, but for the purposes I need these points for, I need to use QGIS and its plugins.

This is a sample within the csv file:

location xlat ylon
2ndary sc -3.9115 35.78992
nd water -3.91238 35.78988
lily pond -3.91587 35.78909
old pump -3.91706 35.7888
drk pump -3.91691 35.78885
fatuma s -3.90755 35.81442
kaku shop-3.90756 35.81433
parts shop-3.90743 35.81444
aquilina h -3.90785 35.81469
diana hou-3.90909 35.81462
traffic sur -3.90654 35.81398
goat wo -3.90938 35.81441
lucian ho -3.91062 35.81454
lucas hou -3.87172 35.82846

marked as duplicate by AndreJ qgis Mar 26 '16 at 19:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Could you post a sample of your csv? – Nathan W Mar 25 '16 at 11:57
  • This is what the first half of the csv looks like, and like I said, shows up correctly in the attribute table when dragging and dropping: location xlat ylon 2ndary sc -3.9115 35.78992 nd water -3.91238 35.78988 lily pond -3.91587 35.78909 old pump -3.91706 35.7888 drk pump -3.91691 35.78885 fatuma s -3.90755 35.81442 kaku shop-3.90756 35.81433 parts shop-3.90743 35.81444 aquilina h -3.90785 35.81469 diana hou-3.90909 35.81462 traffic sur -3.90654 35.81398 goat wo -3.90938 35.81441 lucian ho -3.91062 35.81454 lucas hou -3.87172 35.82846 – Vince Mar 25 '16 at 12:28
  • 1
    @Vince - have you check this ? Its so often, missing 1 check mark or other thing while importing CSV through delimited text layer produce something wrong. – Uje Indo Mar 25 '16 at 12:57
  • Please paste the csv as a edit to your post. – Nathan W Mar 25 '16 at 13:01
  • 1
    @UjeIndo thanks so much! I took the advice in the comments on that page and saved it again as a Windows Comma Separated and that worked perfectly. I guess using a Mac has its downsides and I'll have to make sure to save them all in that format from now on. Thanks everyone! – Vince Mar 26 '16 at 11:05
4

Your csv looks a bit weird:

location xlat ylon
2ndary sc -3.9115 35.78992
nd water -3.91238 35.78988
lily pond -3.91587 35.78909
old pump -3.91706 35.7888
drk pump -3.91691 35.78885
fatuma s -3.90755 35.81442
kaku shop-3.90756 35.81433
parts shop-3.90743 35.81444
aquilina h -3.90785 35.81469
diana hou-3.90909 35.81462
traffic sur -3.90654 35.81398
goat wo -3.90938 35.81441
lucian ho -3.91062 35.81454
lucas hou -3.87172 35.82846

It seems that you want blanks as delimiter, but this does not allow for blanks in the location text. You might need comma or semikolon as delimiter.

Furthermore, kaku shop and parts shop terminate without a blank to the following negative coordinate.

It seems that you have to sanitize your data first.

  • The csv file itself doesn't have the weird spacing my sample does here. That being said, what do you mean the two shops "terminate w/o a blank to the following negative coordinate"? – Vince Mar 26 '16 at 10:54
  • kaku shop-3.90756 has no blank between the location text and the coordinate. But you should take care about blanks within the location text anyway. – AndreJ Mar 26 '16 at 19:34
4

As Andre has shown the CSV file is not consistently formatted. If this file should be dynamically linked it MUST be cleaned. Otherwise reading the file with a Python script and parsing each line might help.

That is what follwing code does:

import re

# define pattern
pattern = re.compile('^(.*) ?(-.*) (.*)$', re.IGNORECASE)

# create target layer
layer = QgsVectorLayer('Point?crs=EPSG:4326&field=location:string', 'locations', 'memory')
prov = layer.dataProvider()
fields = prov.fields()

# list for new features
feats = []

# read file and split into lines
with open('points.csv') as f:
    locs = f.read().splitlines()

    # skip heading
    for loc in locs[1:]:

        # parse line
        r = pattern.search(loc)
        try:
            if r:
                location, xlon, ylat = r.groups()

                # create point feature
                feat = QgsFeature(fields)
                feat.setGeometry(QgsGeometry().fromPoint(QgsPoint(float(xlon), float(ylat))))
                feat['location'] = location
                feats.append(feat)
        except:
            continue

# add all feats
prov.addFeatures(feats)
# add result to legend
QgsMapLayerRegistry.instance().addMapLayer(layer)

The result processing the given CSV data as rewritten by AndreJ:

the imported CSV file

1

I've ran into a similar issue in Arc Map before, the issue was that even though I had a table with information I did not have any geometry attached to the shape file. It may not be the answer but this could be a place to look. I have not used QGIS myself.

1

Looks like the issue was using a Mac, and saving the coordinates as a Windows Comma Separated instead of a regular csv worked perfectly. Thanks for helping everyone!

  • 2
    just a last comment on the file: Your header names are xlat and ylon... The Y-coordinate is normally the latitude and x the longitude. Don't mix this up! – Riccardo Mar 26 '16 at 11:32
  • So it is a duplicate of gis.stackexchange.com/questions/137260/… – AndreJ Mar 26 '16 at 19:37

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