Import vector from CSV


#1

Hi
I have a CSV file containing WKT and I like to import them using snappy or GPT. For now I have solved my problem by writing a simple code. But I am interested to find a better solution like what we have for Import-vector of shape files:
-> vector = snappy.GPF.createProduct(‘Import-Vector’, parameters, product)
I have searched forum and did not find any so created this.


#2

Probably what @giannis posted can help you:


#3

Dear @marpet
Thank you very much for responding. I am looking for equivalent of SNAP’s vector from CSV in SNAPPY or GPT . I can not find the relevance of this post with.


#4

I thought you wanted to read the WKT. That’s why i pointed to the other post.
For reading the CSV file I would suggest to simply use python: https://docs.python.org/3/library/csv.html
It is also possible to use snappy but this is more complicated.


#5

Dear @marpet
Thank you for clarification. As mentioned, I am using this simple code to import.

                with open(file_name, 'r') as csvfile:
                csv_read = csv.reader(csvfile, delimiter=delimiter)
                for i in range(NumHeaderLine):
                    next(csv_read, None)  # skip the headers
                for row in csv_read :
                    if row:
                        wkt = row[1]
                        geom = WKTReader().read(wkt)
                        product = addGeom2Prod(product, geom, geom_name)
            return product

Before that I was using this.

           _with open(CsvFilNm, 'r') as csvfile:
            csv_read = csv.reader(csvfile, delimiter=delimiter)
            for i in range(NumHeaderLine):
                next(csv_read, None)  # skip the headers
            for row in csv_read :
                if row:
                    dataPart = row[1]
                    wkt += dataPart + ' \n'
                    dataPart = []
       geom = snappy.WKTReader().read(wkt)
        return geom

But this one just import the first line of CSV and obviously the wkt with multiple line is not accepted by WKTReader. Any suggestion?


#6

Maybe the geom_name is always the same?


#7

Snap saves each polygon in one line of a CSV file in vector folder and geom_name is the name of CSV file containing that polygons.


#8

Maybe this code snippet is helping you:


You need to put each WKT read from one file into a VectorDataNode. This VDN can have the name of the file.
For the next CSV file you can then create another VDN.