mbloch / mapshaper

Tools for editing Shapefile, GeoJSON, TopoJSON and CSV files
http://mapshaper.org
Other
3.78k stars 535 forks source link

Combine layers, merge and export flatten fields as csv #596

Closed 2803media closed 1 year ago

2803media commented 1 year ago

Hi I have two files:

communes.shp

[info] 
========================================
Layer:    communes
----------------------------------------
Type:     polygon
Records:  35,084
Bounds:   -178.181632,-85.05,168.133656,51.088756
CRS:      +proj=longlat +datum=WGS84
Source:   communes.shp

Attribute data
------------+---------------------------
 Field      | First value
------------+---------------------------
 code       | '01001'
 commune    | ''
 departemen | '01'
 epci       | '200069193'
 nom        | 'L'Abergement-Clémenciat'
 plm        | null
 region     | '84'
------------+---------------------------

And PLU_0.json

[info] 
==================================================
Layer:    PLU_0
--------------------------------------------------
Type:     polygon
Records:  10,000
Bounds:   -3.887635349735755,42.40435023605635,7.468601617593436,49.71331516396345
CRS:      +proj=longlat +datum=WGS84
Source:   PLU_0.json

Attribute data
------------+-------------------------------------
 Field      | First value
------------+-------------------------------------
 bbox       | [-1.704698202614825,49.60026075567804,-1.7041641314805869,49.60062217598128]
 datappro   | '20101213'
 datvalid   | '20101213'
 destdomi   | '08'
 FID        | 'zone_urba.35'
 gid        | 35
 idurba     | null
 idzone     | null
 insee      | '50187'
 lib_idzone | null
 libelle    | '1N'
 libelong   | null
 nomfic     | '50187_R_N.pdf'
 partition  | 'DU_50187'
 typezone   | 'N'
 urlfic     | ''
------------+-------------------------------------

I want to clip communes with PLU_0 and export the result as a CSV file, so I use this command:

mapshaper -i communes.shp PLU_0.shp combine-files \
     -merge-layers force \
    -clip PLU_0.json \
    -drop geometry \
    -filter-fields code,epci,partition \
    -o PLU_0.csv format=csv

This works but the CSV file is made of the layer information from the first layer and after information from the second one: Screenshot 2023-07-25 at 10 33 24

How I can "flatten" the information per item in order to have "code,epci,partition" with the relation in the same feature

Thanks for your help

mbloch commented 1 year ago

The clip command does not transfer attribute data from the clipping shapes to the target shapes (it sounds like this is what you want to do). You can try the -union command, which takes two target layers and combines them into a single output layer, while retaining attributes from both layers. After running -union, you would use -filter to retain only those shapes that have attributes from both of the input layers. Try running -union target=PLU_0,communes in the web UI console and inspect the result... that may put you on the right track.

2803media commented 1 year ago

@mbloch great thanks this is working as expected !

mapshaper -i communes.shp -i PLU_0.json \
    -union target=PLU_0,communes \
    -filter 'partition != null' \
    -o PLU_0.shp

Screenshot 2023-07-28 at 07 21 12