{"id":4789,"date":"2020-05-31T20:00:06","date_gmt":"2020-05-31T19:00:06","guid":{"rendered":"https:\/\/thinkr.fr\/abcdr\/?p=4789"},"modified":"2020-05-31T20:01:07","modified_gmt":"2020-05-31T19:01:07","slug":"comment-lire-un-fichier-shp-shapefile-avec-sf-dans-r","status":"publish","type":"post","link":"https:\/\/thinkr.fr\/abcdr\/comment-lire-un-fichier-shp-shapefile-avec-sf-dans-r\/","title":{"rendered":"Comment lire un fichier shp (shapefile) avec {sf} dans R ?"},"content":{"rendered":"\n<p><strong>Quand on lit un fichier shp ou fichier <em>shapefile<\/em>, on lit un fichier de cartographie de type vecteur<\/strong>. On utilisera le package <code>{sf}<\/code> pour lire ces fichiers de carte. En r\u00e9alit\u00e9, une couche spatiale au format <em>shapefile<\/em> doit \u00eatre distribu\u00e9e avec au minimum 4 fichiers du m\u00eame nom, avec les extension shp, shx, dbf, prj. Sans ces 4 fichiers, vous ne pourrez probablement pas afficher votre carte correctement.<\/p>\n<p>Regardons les fichiers inclus dans le package <code>{sf}<\/code> , vous remarquerez qu&rsquo;ils viennent tous par 4 :<\/p>\n<pre lang=\"r\"><code>list.files(system.file(\"shape\", package=\"sf\"))<\/code><\/pre>\n<pre>[1] \"nc.dbf\" \"nc.prj\" <br \/>[3] \"nc.shp\" \"nc.shx\" <br \/>[5] \"olinda1.dbf\" \"olinda1.prj\" <br \/>[7] \"olinda1.shp\" \"olinda1.shx\" <br \/>[9] \"storms_xyz_feature.dbf\" \"storms_xyz_feature.shp\" <br \/>[11] \"storms_xyz_feature.shx\" \"storms_xyz.dbf\" <br \/>[13] \"storms_xyz.shp\" \"storms_xyz.shx\" <br \/>[15] \"storms_xyzm_feature.dbf\" \"storms_xyzm_feature.shp\"<br \/>[17] \"storms_xyzm_feature.shx\" \"storms_xyzm.dbf\" <br \/>[19] \"storms_xyzm.shp\" \"storms_xyzm.shx\"<\/pre>\n<p>Voici comment on lirait les fichiers nomm\u00e9s <code>nc.*<\/code> <em>s&rsquo;ils \u00e9taient dans le m\u00eame dossier que notre script R<\/em>. En r\u00e9alit\u00e9, on ne lit que le fichier nomm\u00e9 <code>nc.shp<\/code>, les autres sont lus en m\u00eame temps par la fonction <code>st_read()<\/code> :<\/p>\n<pre lang=\"r\"><code>nc &lt;- st_read(\"nc.shp\")<\/code><\/pre>\n<p>Ensuite, vous pourrez directement afficher la carte avec la m\u00e9thode <code>plot()<\/code> :<\/p>\n<pre lang=\"r\"><code>plot(nc)<\/code><\/pre>\n<p>Pour tester sur votre ordinateur un exemple reproductible, vous pouvez tester ce script :<\/p>\n<pre lang=\"r\"><code>library(sf)\nnc = st_read(system.file(\"shape\/nc.shp\", package=\"sf\"))\nplot(nc)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"367\" data-attachment-id=\"4790\" data-permalink=\"https:\/\/thinkr.fr\/abcdr\/comment-lire-un-fichier-shp-shapefile-avec-sf-dans-r\/rplot\/\" data-orig-file=\"https:\/\/thinkr.fr\/abcdr\/wp-content\/uploads\/2020\/05\/Rplot.png\" data-orig-size=\"400,367\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Rplot\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/thinkr.fr\/abcdr\/wp-content\/uploads\/2020\/05\/Rplot.png\" src=\"http:\/\/thinkr.fr\/abcdr\/wp-content\/uploads\/2020\/05\/Rplot.png\" alt=\"\" class=\"wp-image-4790\" srcset=\"https:\/\/thinkr.fr\/abcdr\/wp-content\/uploads\/2020\/05\/Rplot.png 400w, https:\/\/thinkr.fr\/abcdr\/wp-content\/uploads\/2020\/05\/Rplot-300x275.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Quand on lit un fichier shp ou fichier shapefile dans R, on lit un fichier de cartographie de type vecteur.<a class=\"more-link\" href=\"https:\/\/thinkr.fr\/abcdr\/comment-lire-un-fichier-shp-shapefile-avec-sf-dans-r\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":{"twitter_399453572_399453572":""},"rop_publish_now_history":[],"rop_publish_now_status":"pending","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,20],"tags":[33,35,34],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-seb","4":"has-excerpt","5":"post-4789","7":"format-standard","8":"category-graphique","9":"category-sig-cartographie","10":"post_tag-sf","11":"post_tag-shapefile","12":"post_tag-shp"},"acf":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p9O7Sx-1ff","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/posts\/4789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/comments?post=4789"}],"version-history":[{"count":2,"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/posts\/4789\/revisions"}],"predecessor-version":[{"id":4792,"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/posts\/4789\/revisions\/4792"}],"wp:attachment":[{"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/media?parent=4789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/categories?post=4789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thinkr.fr\/abcdr\/wp-json\/wp\/v2\/tags?post=4789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}