msrajwat298
msrajwat298

Reputation: 493

How to know layer name from anyone of files map.dbf map.prj map.shp map.shx using go

I am working on the project where we are giving the user to upload GIS layers and it should not be duplicate. what I got to know that user can upload same file multiple time just rename these file (map.dbf map.prj map.shp map.shx).

So I want to know, there is any way that I can know the layer name by extracting the data from any one of file.

I tried to extract the data from .shp file but I got only attribute not actual layer name.

package main

import (
    "log"
    "os"

    "github.com/jonas-p/go-shp"
)

func main() {
    // Create a log file
    logFile, err := os.OpenFile("shapefile.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
    }
    defer logFile.Close()

    // Set the output of the logger to the log file
    log.SetOutput(logFile)

    // Open a shapefile for reading
    shapeReader, err := shp.Open("map.shp")
    if err != nil {
        log.Fatal(err)
    }
    defer shapeReader.Close()

    // Loop through all features in the shapefile
    for i := 0; shapeReader.Next(); i++ {
        // Print all fields for each feature
        for k, f := range shapeReader.Fields() {
            val := shapeReader.ReadAttribute(i, k)
            log.Printf("%v: %v\n", f, val)
        }
    }
}

Upvotes: 4

Views: 68

Answers (2)

Pankaj_Saini
Pankaj_Saini

Reputation: 98

.shp and .dbf files only contains coordinates; they do not contain actual file name. So to avoid duplication of data, we can go with hashing the file content. We can generate a unique identifier that we can compare with other files to determine if they are identical or not. It helps to avoid duplication of data, even if file names are different.

Upvotes: 1

Ian Turton
Ian Turton

Reputation: 10976

ShapeFiles don't have a name other than the name of the file. I suspect that you need to keep track of layer names and add a new suffix to the layer name if they upload the file a second time.

To be honest, you'll face the same issue no matter what data format you use as users often add a table twice.

Upvotes: 2

Related Questions