1

The problem is solved with a proj file. I created a shapefile from a csv file. I used ogrinfo and appears Layer SRS WKT: unknown.

Technologies

  • Python 2.7.9
  • pyshp compatible with Python 2.7
  • proj 4.8.0, 6 March 2012
  • geos 3.4.2
  • gdal
  • django 1.11 and GeoDjango

Sample of the csv file

Name;Latitude;Longitude;
ALAVA SALIDAS;42.842003;-2.740091
TIPSA FUENLABRADA LLEGADAS;40.266063;-3.848882

Python code

import shapefile as shp import csv, sys

class convertCsvToShp:
def __init__(self, csv_path):
    self.csv_path=csv_path

# funtion to generate a .prj file
def getWKT_PRJ (self, epsg_code):
    import urllib
    wkt = urllib.urlopen("http://spatialreference.org/ref/epsg/{0}/prettywkt/".format(epsg_code))
    remove_spaces = wkt.read().replace(" ","")
    output = remove_spaces.replace("\n", "")
    return output

def code(self):

    try:
        # Set shapefile writer

        w = shp.Writer()
        w.autoBalance = 1

        # Fields are the city, the latitude and the longitude.
        w.field("name", "C", 50) # C are ASCII characters.
        w.field("latitude", "F", 10, 6)
        w.field("longitude", "F", 10, 6)

        # Readeing data from csv file
        with open(self.csv_path + 'coord.csv', 'r') as csvfile:
            reader = csv.reader(csvfile, delimiter=';')
            next (reader, None) # Skip the header of the csv

            for row in reader:
                # Create the point of the geometry
                w.point(float(row[1]), float(row[2]))
                # Attributes of the data
                w.record(row[0], row[1], row[2])

        w.save(self.csv_path + 'agencies')

        # create a projection file
        prj = open(self.csv_route + 'agencies.prj', 'w')
        epsg = self.getWKT_PRJ("4326")
        prj.write(epsg)
        prj.close()
        return True
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise

ogrinfo

ogrinfo -so csvtoshp/static/files/agencies.shp agencies

enter image description here

Problem

The problem is that openlayers do not show the coordinates correctly. I do not know that this Layers SRS WKT unknown is involved. It is required a proj file? Yes, it is required it.

New ogrinfo

enter image description here

closed as off-topic by Vince, xunilk, Midavalo Mar 25 '18 at 21:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Vince, xunilk, Midavalo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • see Pyshp: Create a .prj Projection File for a Shapefile or use another module as Fiona or GeoPandas – gene Mar 25 '18 at 11:49
  • Ok, thanks you very much. In consideration of your advice, I created prj file in my code. The ogrinfo works. I am going to write my solution. – mangorex Mar 25 '18 at 13:02
  • I wrote the solution. A function getWKT_PRJ (self, epsg_code) to take epsg_code and the creation of the prj inside the function code.I decided to use leaflet and leave openalayers. It is not fixed muy problem with the coordinates of the map. I will put another posts. The issue of this post is fixed. – mangorex Mar 25 '18 at 13:13
  • 2
    Please self-answer your question in the answer section below. – AndreJ Mar 25 '18 at 17:59