0

enter image description hereI have multiples CSV files. Every CSV file contains 365 days of different weather parameters for different location. I want to take specific month from every csv row and export it to point shp file using ArcPy. Can you help me to do it using ArcPy. In csv date format is (MM DD YY) Below i have attached my python code.This code giving me error while i running date specific

import arcpy,os
... shpworkspace = r"D:\SWAT-WEATHER-DATA2"
... arcpy.env.workspace = shpworkspace
... arcpy.env.overwriteOutput = True
... 
... csvlist = arcpy.ListFiles("*.csv")
... 
... for csvfile in csvlist:
    print csvfile
    outlayer = "CSVEventLayer"
    spatialreference = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
    arcpy.MakeXYEventLayer_management(csvfile,"Longitude","Latitude","Date 1/1/2013-1/31/2013",outlayer,spatialreference,"#")

    shpfile = os.path.splitext(csvfile)[0].replace('-','_')        
    arcpy.CopyFeatures_management(outlayer,shpfile)

    del outlayer

I have edited my code based on suggestions from different expert. Still i am getting error. Kindly guide me to solve this issue.

import arcpy,os
... shpworkspace = r"D:\SWAT-WEATHER-DATA4"
... arcpy.env.workspace = shpworkspace
... arcpy.env.overwriteOutput = True
... 
... csvlist = arcpy.ListFiles("*.csv")
... 
... for csvfile in csvlist:
    print csvfile
    outlayer = "CSVEventLayer"
    spatialreference = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
    arcpy.MakeXYEventLayer_management(csvfile,"Longitude","Latitude",outlayer,spatialreference,"#")
    arcpy.SelectLayerByAttribute_management(outlayer, "NEW_SELECTION", "Date" >= date "01/01/2013 00:00:00" AND "Date" < date "01/31/2013 00:00:00")  
    shpfile = os.path.splitext(csvfile)[0].replace('-','_')    
    arcpy.CopyFeatures_management(outlayer,shpfile)

    del outlayer
  • 1
    Welcome to GIS SE. As this question is currently framed, it is not possible to answer the question. Please edit your question to include the following items: 1) what have you tried? 2) where have your efforts failed? 3) What type of geometry do you require (Point, Line, Polygon, etc), 4) A sample (subset) of the csv data, 5) a sample of the desired output. Once these items are addressed, you can vote to reopen the question. – Aaron Aug 1 '16 at 11:50
  • 1
    Welcome to GIS SE. I note that you haven't yet taken the Tour, which explains our general polices. While the volunteers here are quite generous with their time to help fix Python code, they need a code framework to fix. This is in addition to the as-yet unaddressed aspects of Aaron's comment. – Vince Aug 1 '16 at 12:07
  • Sir i have given the details of my code which is taking all the date from csv file . but i want some temporal extraction from csv (e.g month) – Shouvik Jha Aug 1 '16 at 12:25
  • What error are you getting? It looks like you have too many arguments in MakeXYEventLayer (7 instead of 6). To the best of my knowledge, you cannot select a date range right within the tool -- is that what you are trying to do? – J Kelly Aug 1 '16 at 13:18
  • 1
    Ok, but that is just the dates. You want more info than that, right? You want something like the lat and lon and other data associated with particular dates? There must be more to your csv lines than that. Something like 1/1/2013, 32.124, -79.111, 1/2/2013, 32.116, -79.110, 1/3/2013 etc. I am trying to guide you towards making a copy csv containing only the points corresponding to the dates you want, and then performing MakeXYEventLayer only on the good stuff. – J Kelly Aug 1 '16 at 14:11
1

Remove "Date 1/1/2013-1/31/2013" from the arguments of MakeXYEventLayer. Add something like:

   arcpy.SelectLayerByAttribute_management(outlayer, "NEW_SELECTION", ' "Date" = <whatever> ')  
# This selects the points with the right date.

    arcpy.CopyFeatures_management(outlayer, shpfile) 
# This copies the selected ones into an .shp. 

This is just a structure to guide you. The field name may not actually be "Date." You will need to inspect the output of MakeXYEventLayer and/or put header rows into the csv's to define the field names.

  • Thank you sir . Plz tell me how to define the range of date here. it would be like this ("Date" = <1/1/2013-1/31/2013> '). Suppose i want to extract only month of January from daily data. in CSV file date format is e.g 1/1//2013, 1/2/2013, 1/3/2013 so on......... I am new in python. Not have much knowledge . – Shouvik Jha Aug 1 '16 at 16:25
  • I am reconsidering this approach. It sounds like each XYEventLayer is only one point. Select selects rows (you probably only have one). You want a column value. – J Kelly Aug 1 '16 at 16:27
  • Do me a favor. Run MakeXYEventLayer without the Date stuff. Open what you get in ArcMap and then open its attribute table. Can you paste an image of what it looks like? – J Kelly Aug 1 '16 at 16:28
  • I want to extract specific range of date along with all field of data from csv. – Shouvik Jha Aug 1 '16 at 16:29
  • Sir , I have added the snapshot of arc GIS in my first command – Shouvik Jha Aug 1 '16 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.